kotlin和ArgumentCaptor – IllegalStateException

我有一个通过ArgumentCaptor捕获类参数的问题。 我的测试类看起来像这样:

@RunWith(RobolectricGradleTestRunner::class) @Config(sdk = intArrayOf(21), constants = BuildConfig::class) class MyViewModelTest { @Mock lateinit var activityHandlerMock: IActivityHandler; @Captor lateinit var classCaptor: ArgumentCaptor<Class> @Captor lateinit var booleanCaptor: ArgumentCaptor 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); } } 

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

=========更新1:
Kotlin:1.0.0-beta-4584
Mockito:1.10.19
Robolectric: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) 

classCaptor.capture()的返回值为null,但IActivityHandler#navigateTo(Class, Boolean)的签名不允许null参数。

mockito-kotlin库提供了解决这个问题的支持function。

对于某些人来说,在Android Architecture回购中有一个由Google提供的文件MockitoKotlinHelpers.kt 。 它提供了一个方便的方法来调用捕获..只需调用verify(activityHandlerMock).navigateTo(capture(classCaptor), capture(booleanCaptor))

根据这个解决方案,

 fun  uninitialized(): T = null as T //open verificator val verificator = verify(activityHandlerMock) //capture (would be same with all matchers) classCaptor.capture() booleanCaptor.capture() //hack verificator.navigateTo(uninitialized(), uninitialized()) 

在kotlin-Mockito图书馆没有帮助之后来到这里。 我用reflection创建了一个解决方案。 这是一个函数,提取提供给模拟对象的论点:

 fun  getTheArgOfUsedFunctionInMockObject(mockedObject: Any, function: (T) -> S, clsOfArgument: Class): T{ val argCaptor= ArgumentCaptor.forClass(clsOfArgument) val ver = verify(mockedObject) argCaptor.capture() ver.javaClass.methods.first { it.name == function.reflect()!!.name }.invoke(ver, uninitialized()) return argCaptor.value } private fun  uninitialized(): T = null as T 

用法:(说我嘲笑我的资源库并测试了一个viewModel,用MenuObject对象调用viewModel的“update()”方法后,我想确保MenuObject实际调用存储库的“updateMenuObject()”方法:

 viewModel.update(menuObjectToUpdate) val arg = getTheArgOfUsedFunctionInMockObject(mockedRepo, mockedRepo::updateMenuObject, MenuObject::class.java) assertEquals(menuObjectToUpdate, arg)