doReturn不作为代理,但调用我的方法

我正在实施一些unit testing在我的应用程序,我有一个依赖于依赖于Application上下文。 该依赖返回一个Item列表,但我想嘲笑该逻辑返回任何我想要的。

我已经知道的区别:

a) when(foo.method1()).return(bar)

b) doReturn(bar).when(foo).method1()

(b)不应调用该方法。

现在我有一个在ItemHelper.kt调用的方法:

 fun retrieveItems(): MutableList { val boxStore = BoxStore.getInstance().getBoxFor(Item::class.java) return boxStore.all } 

.getInstance()依赖于Application

既然我想嘲笑它,这是我的考验:

 class ItemHelperTests { @JvmField @Rule var mockitoRule = MockitoJUnit.rule()!! @Mock private lateinit var itemHelper: ItemHelper @Test fun itemsNumber_Test() { Mockito.doReturn(ArrayList()).`when`(itemHelper).retrieveItems() System.out.println("this line is unreachable") } } 

但是结束调用我的代码:

 java.lang.Exception: Please init BoxStore.boxStore in the MainApplication at com.example.dependencies.coreData.BoxStore$Companion.throwBoxStoreNotInitialized(BoxStore.kt:33) at com.example.dependencies.coreData.BoxStore$Companion.getInstance(BoxStore.kt:26) at com.example.helpers.coreData.Item.ItemHelper.retrieveItems(ItemHelper.kt:20) at com.example.helpers.ItemHelperTests.itemsNumber_Test(ItemHelperTests.kt:27) 

所以这个方法被调用,我只想从JVM调用该方法时检索一个空的ArrayList()


编辑

当使用@RunWith(MockitoJUnitRunner::class)

我得到了同样的错误,但是我又多了一行Mockito错误:

 java.lang.Exception: Please init BoxStore.boxStore in the MainApplication at com.example.dependencies.coreData.BoxStore$Companion.throwBoxStoreNotInitialized(BoxStore.kt:33) at com.example.dependencies.coreData.BoxStore$Companion.getInstance(BoxStore.kt:26) at com.example.helpers.coreData.Item.ItemHelper.retrieveItems(ItemHelper.kt:20) at com.example.helpers.ItemHelperTests.itemsNumber_Test(ItemHelperTests.kt:27) org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at com.example.helpers.ItemHelperTests.itemsNumber_Test(ItemHelperTests.kt:27) Eg thenReturn() may be missing. Examples of correct stubbing: when(mock.isOk()).thenReturn(true); when(mock.isOk()).thenThrow(exception); doThrow(exception).when(mock).someVoidMethod(); Hints: 1. missing thenReturn() 2. you are trying to stub a final method, which is not supported 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed at org.mockito.internal.runners.DefaultInternalRunner$1$1.testFinished(DefaultInternalRunner.java:69) at org.junit.runner.notification.SynchronizedRunListener.testFinished(SynchronizedRunListener.java:56) at org.junit.runner.notification.RunNotifier$7.notifyListener(RunNotifier.java:190) at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:72) at org.junit.runner.notification.RunNotifier.fireTestFinished(RunNotifier.java:187) at org.junit.internal.runners.model.EachTestNotifier.fireTestFinished(EachTestNotifier.java:38) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:331) 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.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:78) at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:84) at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39) at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161) 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:51) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 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 com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131) 

在Kotlin,每个课程和方法都是封闭的。

我必须在retrieveItems()之前定义open ,剩下的:

 open fun retrieveItems(): MutableList { val boxStore = BoxStore.getInstance().getBoxFor(Item::class.java) return boxStore.all }