I'm trying to make testing library for mocking LocalDate.now() for checking correct date tests
My code is:
```kotlinfun mockSetLocalDateNow(date: String) {
mockkStatic(LocalDate::class)
every { LocalDate.now() } returns LocalDate.parse(date)
}
### Current Behavior
Test doesn't work
### Failure Information (for bugs)
io.mockk.MockKException
#### Steps to Reproduce
Please provide detailed steps for reproducing the issue.
Just run the tests
#### Context
Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
* MockK version: 1.9.3
* OS: Windows 10 x64
* Kotlin version: 1.3.50
* JDK version: jdk1.8.0_221
* JUnit version: 4.12
* Type of test: unit test
#### Stack trace
io.mockk.MockKException: every/verify {} block were run several times. Recorded calls count differ between runs
Round 1: class java.time.LocalDate.of(-999999999, 1, 1), class java.time.LocalDate.of(999999999, 12, 31), class java.time.LocalDate.now()
Round 2: class java.time.LocalDate.now()
at io.mockk.impl.recording.SignatureMatcherDetector$detect$1.invoke(SignatureMatcherDetector.kt:25)
at io.mockk.impl.recording.SignatureMatcherDetector.detect(SignatureMatcherDetector.kt:86)
at io.mockk.impl.recording.states.RecordingState.signMatchers(RecordingState.kt:39)
at io.mockk.impl.recording.states.RecordingState.round(RecordingState.kt:31)
at io.mockk.impl.recording.CommonCallRecorder.round(CommonCallRecorder.kt:50)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:59)
at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
at io.mockk.MockKDsl.internalEvery(API.kt:92)
at io.mockk.MockKKt.every(MockK.kt:104)
at com.mitya1234.testing.MockDateTests.mockSetLocalDateNow(MockDateTests.kt:11)
at com.mitya1234.testing.MockDateTests.testMockedDate(MockDateTests.kt:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
#### Minimal reproducible code (the gist of this issue)
```kotlin
Maven:
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<version>1.9.3</version>
</dependency>
package com.mitya1234.testing
import org.junit.Test
import io.mockk.every
import io.mockk.mockkStatic
import java.time.LocalDate
class MockDateTests {
fun mockSetLocalDateNow(date: String) {
mockkStatic(LocalDate::class)
every { LocalDate.now() } returns LocalDate.parse(date)
}
@Test
fun testMockedDate() {
val date = "2019-01-01"
mockSetLocalDateNow(date)
val result = LocalDate.now() == LocalDate.parse(date)
println(result)
assert(result)
}
}
I鈥檝e forgot to init. My mistake
@DmitriiTrifonov Could you elaborate? What do you mean by init?
@calvarez-ov i think he mean initialise AndroidThreeTen in Application
For example :
`class FakeApplication: Application() {
override fun onCreate() {
super.onCreate()
startKoin {
// Android context
androidContext(this@FakeApplication)
// modules
modules(listOf())
}
AndroidThreeTen.init(this)
}
}`
Most helpful comment
@DmitriiTrifonov Could you elaborate? What do you mean by init?