【问题标题】:java.lang.reflect.InvocationTargetException when using Kotlin-Mokito library使用 Kotlin-Mokito 库时出现 java.lang.reflect.InvocationTargetException
【发布时间】:2016-09-19 17:37:21
【问题描述】:

来自 Kotlin Mockito 库的 any() 使用以下代码崩溃

测试类

import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.verify
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations

class SimpleClassTest {

    lateinit var simpleObject: SimpleClass
    @Mock lateinit var injectedObject: InjectedClass


    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)
    }

    @Test
    fun testSimpleFunction() {
        simpleObject = SimpleClass(injectedObject)
        simpleObject.simpleFunction()

        verify(injectedObject).settingDependentObject(any())

    }
}

源类

import com.squareup.okhttp.Protocol
import com.squareup.okhttp.Request
import com.squareup.okhttp.Response


class SimpleClass(val injectedClass: InjectedClass) {

    fun simpleFunction() {
        injectedClass.settingDependentObject(Response.Builder()
                .request(Request.Builder().url("https://example.com").build())
                .code(200)
                .body(null)
                .protocol(Protocol.HTTP_1_1)
                .build())
    }
}

open class DependentClass(response: Response) {

}

open class InjectedClass() {
    lateinit var response: Response

    open fun settingDependentObject(response: Response) {
        this.response = response
    }
}

崩溃日志

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at kotlin.reflect.jvm.internal.FunctionCaller$Constructor.call(FunctionCaller.kt:63)
    at kotlin.reflect.jvm.internal.KCallableImpl$DefaultImpls.call(KCallableImpl.kt:67)
    at kotlin.reflect.jvm.internal.KFunctionImpl.call(KFunctionImpl.kt:30)
    at kotlin.reflect.jvm.internal.KCallableImpl$DefaultImpls.callBy(KCallableImpl.kt:103)
    at kotlin.reflect.jvm.internal.KFunctionImpl.callBy(KFunctionImpl.kt:30)
    at com.nhaarman.mockito_kotlin.CreateInstanceKt.newInstance(CreateInstance.kt:138)
    at com.nhaarman.mockito_kotlin.CreateInstanceKt.createInstance(CreateInstance.kt:60)
    at com.elyeproj.phoneinfo.SimpleClassTest.testSimpleFunction(SimpleClassTest.kt:36)
    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 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.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    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.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:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.NullPointerException
    at com.squareup.okhttp.Response.<init>(Response.java:56)
    ... 39 more

【问题讨论】:

  • 有一个open PR 可以解决this issue。但是,我不完全确定这是否值得生产。

标签: mockito kotlin


【解决方案1】:

库依赖于反射来实例化给定类型的对象,以用作模拟“任何”对象。 对于那些具有非平凡构造函数的类来说,这可能是不可能的。 Response 就是其中之一。

如果你能以某种方式获得Response 的实例,你可以像这样使用它:

verify(injectedObject).settingDependentObject(Mockito.any<Response>() ?: someResponse)

【讨论】:

  • 是的,这就是问题所在。希望 nharmaan 能在他的图书馆里想办法解决这个问题。
  • Mockito.any&lt;Response&gt;() 将始终返回 null,不是吗?
  • @miensol,是的,这就是为什么之后需要一个具有一些非空响应的 elvis 运算符
  • @Ilya 所以它等同于null ?: someResponse,而后者又等同于someResponse,不是吗?
  • @miensol,不是,因为Mockito.any 是一个有副作用的函数,它会修改被模拟对象使用的内部状态。
猜你喜欢
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
相关资源
最近更新 更多