【问题标题】:doReturn doesn't act as proxy but calls my methoddoReturn 不充当代理,但调用我的方法
【发布时间】:2018-01-19 12:43:32
【问题描述】:

我正在我的应用程序中实现一些单元测试,并且我有一个依赖于 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)

【问题讨论】:

    标签: android unit-testing kotlin mockito


    【解决方案1】:

    在 Kotlin 中,每个类和方法都是封闭的。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      相关资源
      最近更新 更多