监视类的所有实例。我们要做的就是在创建实例之前调用mockkConstructor(MyObject::class)
。现在我想在adnroid Views上做到这一点(在robolectric的帮助下)
像mockkConstructor(MyCustomView::class)
这是我们实例化自己的简单java对象的方法 不像系统实例化的Android视图
以下测试成功通过
@Test
fun mockingConstructor() {
//with
mockkConstructor(Car::class)
//from now all Car instances acts as if they are a <<spy>>
every { anyConstructed<Car>().wheelsCount() } returns 6
//assertions
assertEquals(Car("plate-number1").wheelsCount(), 6) //pass
assertEquals(Car("plate-number2").wheelsCount(), 6) //pass
assertEquals(Car("plate-number3").wheelsCount(), 6) //pass
//other functions work as implemented in their classes like :
assertEquals(Car("plate-number4").drive(), "now driving") //pass
}
问题
这就是我尝试监视失败
的android视图的方法@RunWith(RobolectricTestRunner::class)
class TestClass {
@Test
fun test2() {
mockkConstructor(MyCustomView::class)
//a simple empty activity that uses myCustomView in its layout.xml
val activity = Robolectric.setupActivity(NavTestActivity::class.java)
}
}
错误
这是我得到的错误::
java.lang.StackOverflowError
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.View.resolveLayoutParams(View.java)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7335)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7341)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.View.resolveLayoutParams(View.java)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7335)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7341)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.View.resolveLayoutParams(View.java)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7335)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7341)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.View.resolveLayoutParams(View.java)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7335)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7341)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.View.resolveLayoutParams(View.java)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7335)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7341)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
at io.mockk.impl.stub.ConstructorStub.answer(ConstructorStub.kt:29)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:48)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:224)
at io.mockk.impl.stub.ConstructorStub.handleInvocation(ConstructorStub.kt:70)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:24)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at android.view.View.resolveLayoutParams(View.java)
at android.view.ViewGroup.resolveLayoutParams(ViewGroup.java:7335)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:82)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:19)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:177)
at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:48)
...