Как смоделировать (шпионский) просмотр андроида с помощью библиотеки mockk?
В библиотеке mockk есть способ шпионить за всеми экземплярами класса. и все, что нам нужно сделать, это позвонить 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.StackruError
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)
...