gpt4 book ai didi

mockito - 使用Kotlin-Mokito库时java.lang.reflect.InvocationTargetException

转载 作者:行者123 更新时间:2023-12-02 12:43:59 25 4
gpt4 key购买 nike

来自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

最佳答案

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

如果您可以某种方式获得Response的实例,则可以改为使用它:

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

关于mockito - 使用Kotlin-Mokito库时java.lang.reflect.InvocationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37380830/

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