gpt4 book ai didi

android - kotlin 和 ArgumentCaptor - IllegalStateException

转载 作者:IT老高 更新时间:2023-10-28 13:27:11 24 4
gpt4 key购买 nike

我在通过 ArgumentCaptor 捕获 Class 参数时遇到问题。我的测试类如下所示:

@RunWith(RobolectricGradleTestRunner::class)
@Config(sdk = intArrayOf(21), constants = BuildConfig::class)
class MyViewModelTest {
@Mock
lateinit var activityHandlerMock: IActivityHandler;

@Captor
lateinit var classCaptor: ArgumentCaptor<Class<BaseActivity>>

@Captor
lateinit var booleanCaptor: ArgumentCaptor<Boolean>

private var objectUnderTest: MyViewModel? = null

@Before
fun setUp() {
initMocks(this)
...
objectUnderTest = MyViewModel(...)
}

@Test
fun thatNavigatesToAddListScreenOnAddClicked(){
//given

//when
objectUnderTest?.addNewList()

//then
verify(activityHandlerMock).navigateTo(classCaptor.capture(), booleanCaptor.capture())
var clazz = classCaptor.value
assertNotNull(clazz);
assertFalse(booleanCaptor.value);
}
}

当我运行测试时,抛出以下异常:
java.lang.IllegalStateException: classCaptor.capture() 不能为空
是否可以在 kotlin 中使用参数捕获器?

=========更新 1:
Kotlin:1.0.0-beta-4584
模拟:1.10.19
机器人电动:3.0

=========更新 2:
堆栈跟踪:

java.lang.IllegalStateException: classCaptor.capture() must not be null

at com.example.view.model.ShoplistsViewModelTest.thatNavigatesToAddListScreenOnAddClicked(ShoplistsViewModelTest.kt:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
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.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
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.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
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:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

最佳答案

从此blog

“让匹配器与 Kotlin 一起工作可能是一个问题。如果你有一个用 kotlin 编写的方法,它不接受可为空的参数,那么我们无法使用 Mockito.any() 来匹配它。这是因为它可以返回 void并且这不能分配给不可为空的参数。如果要匹配的方法是用 Java 编写的,那么我认为它将起作用,因为所有 Java 对象都可以隐式地为空。"

需要一个将 ArgumentCaptor.capture() 作为可空类型返回的包装函数。

将以下内容作为辅助方法添加到您的测试中

fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture()

请看,MockitoKotlinHelpers.kt由 Google 在 Android Architecture 存储库中提供以供引用。 capture 函数提供了一种方便的方式来调用 ArgumentCaptor.capture()。打电话

verify(activityHandlerMock).navigateTo(capture(classCaptor), capture(booleanCaptor))

更新:如果上述解决方案不适合您,请在下方评论中查看 Roberto Leinardi 的解决方案。

关于android - kotlin 和 ArgumentCaptor - IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34773958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com