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

时间:2018-01-19 12:43:32

标签: android unit-testing kotlin mockito

我在我的应用中实现了一些单元测试,并且我依赖于Application上下文。该依赖项返回Item的列表,但我想模仿该逻辑以返回我想要的任何内容。

我已经知道了:

之间的区别

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

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

(b)不应该调用该方法。

现在我有一个名为ItemHelper.kt的方法:

fun retrieveItems(): MutableList<Item> {
    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<Item>()).`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)

E.g. 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)

1 个答案:

答案 0 :(得分:0)

在Kotlin,每个班级和方法都已关闭。

我必须在open之前定义retrieveItems(),其余:

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