I'm running a unit test using mockk. When trying to verify a method, I'm getting an assertionError and I can't figure out how to get the test to run. Here is my test method:
@get:Rule
var rule: TestRule = InstantTaskExecutorRule()
val autoDownloadsManager = mockk<AutoDownloadsManager>(relaxed = true)
@Test
fun `remove downloads when`() {
every { autoDownloadsManager.downloadsNeedsToBeDeletedOnFinishPlay(any(), any()) } just Runs
verify(exactly = 1) { autoDownloadsManager.downloadsNeedsToBeDeletedOnFinishPlay(any(), any()) }
}
The stack trace:
java.lang.AssertionError: Verification failed: call 1 of 1: AutoDownloadsManager(#1).downloadsNeedsToBeDeletedOnFinishPlay(any(), any())) was not called
at io.mockk.impl.recording.states.VerifyingState.failIfNotPassed(VerifyingState.kt:66)
at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:42)
at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
at io.mockk.MockKDsl.internalVerify(API.kt:118)
at io.mockk.MockKKt.verify(MockK.kt:139)
at io.mockk.MockKKt.verify$default(MockK.kt:136)
at media.luminary.datastore.downloads.AutoDownloadsTest.remove downloads when(AutoDownloadsTest.kt:141)
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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
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)
the method in code:
fun downloadsNeedsToBeDeletedOnFinishPlay(podcastUuid: String, uuid: String) {
when (autoDownloadsPreference.frequencyOfDeletion) {
DownloadRemoveTime.IMMEDIATE -> removeDownload(podcastUuid, uuid)
DownloadRemoveTime.HOUR -> setWorkManager(uuid, DownloadRemoveTime.HOUR.timeInMillis)
DownloadRemoveTime.DAY -> setWorkManager(uuid, DownloadRemoveTime.DAY.timeInMillis)
DownloadRemoveTime.WEEK -> setWorkManager(uuid, DownloadRemoveTime.WEEK.timeInMillis)
DownloadRemoveTime.NEVER -> return
}
}
my setup
compileSdkVersion 29
defaultConfig {
minSdkVersion 19
targetSdkVersion 29
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
MockK version: 1.9
OS: Android
Kotlin version: 1.3.61
JDK version: jdk1.8.0_212
JUnit version: 4.12
Type of test: unit test
verify(exactly = 1) { autoDownloadsManager.downloadsNeedsToBeDeletedOnFinishPlay(any(), any()) }
failed because I never called:
autoDownloadsManager.downloadsNeedsToBeDeletedOnFinishPlay(podcastUuid, uuid)