gpt4 book ai didi

android - AlertDialog 单元测试 - java.lang.VerifyError : Bad type on operand stack

转载 作者:行者123 更新时间:2023-11-29 02:20:56 32 4
gpt4 key购买 nike

我有一个 Activity MyActivity.java,我在其中使用 AlertDialog.Builder(this); 生成一个 AlertDialog
现在,我正在尝试为 MyActivity.java 编写单元测试 (junit),方法是将它视为一个简单的 java 类,并为 Android 的框架类创建影子类。当我尝试运行测试时,我在尝试实例化类时遇到错误:java.lang.VerifyError: Bad type on operand stack

java.lang.VerifyError: Bad type on operand stack
Exception Details:
Location:
com/domain/myApp/MyActivity.myMethod()V @31: invokespecial
Reason:
Type 'com/domain/myApp/MyActivity' (current frame, stack[2]) is not assignable to 'android/content/Context'
Current Frame:
bci: @31
flags: { }
locals: { 'com/domain/myApp/MyActivity', 'android/content/res/Resources' }
stack: { uninitialized 26, uninitialized 26, 'com/domain/myApp/MyActivity' }
Bytecode:
0x0000000: 120f 123a b800 112a b400 03c6 000a 120f
0x0000010: 123b b800 3c2a b600 184c bb00 3d59 2ab7
0x0000020: 003e 4d2a 2c2b 123f b600 40b6 0041 2b12
0x0000030: 42b6 0040 b600 432b 1244 b600 402a b700
0x0000040: 45b6 0046 2b12 47b6 0040 bb00 4859 2ab7
0x0000050: 0049 b600 4abb 004b 592a b700 4cb6 004d
0x0000060: b600 4eb5 0003 2ab4 0003 b600 4f2a b400
0x0000070: 0312 51b6 0052 c000 534e 2db6 0054 57b1
0x0000080:
Stackmap Table:
same_frame(@21)


at com/domain/myApp/MyActivityTest.before(MyActivityTest.java:93)
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.RunBefores.evaluate(RunBefores.java:24)
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.mockito.internal.runners.DefaultInternalRunner$1.run(Unknown Source)
at org.mockito.internal.runners.DefaultInternalRunner.run(Unknown Source)
at org.mockito.junit.MockitoJUnitRunner.run(Unknown Source)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

根据我的研究,发生此错误是因为我试图在静态上下文 (AlertDialog.Builder) 中引用 this。所以基本上我试图在完全构建之前引用 MyActivity

我想如果在我的 AlertDialog 影子类中我将 AlertDialog.Builder 类设为非静态,那么在测试期间就不会出现这个问题。但事实并非如此。有谁知道我怎样才能找到解决方法?

最佳答案

发生这种情况是因为您必须为 Activity 设置影子类。
实际的Activity类扩展了ContextThemeWrapper类,ContextWrapper类扩展了抽象的Context类。

只需创建影子 Context 类并使用它来扩展影子 Activity 类。
这整个错误基本上是不兼容的类型错误。

关于android - AlertDialog 单元测试 - java.lang.VerifyError : Bad type on operand stack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55720762/

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