AlertDialog单元测试-java.lang.VerifyError:操作数堆栈上的类型错误

时间:2019-04-17 05:53:14

标签: android junit static android-alertdialog

我有一个活动MyActivity.java,其中我正在使用AlertDialog生成一个AlertDialog.Builder(this);
现在,我试图通过将junit当作一个简单的Java类并为Android的框架类创建shadow类来编写单元测试(MyActivity.java)。 当我尝试运行测试时,尝试实例化该类时出现错误: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)

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

我发现如果在我的AlertDialog的影子类中使AlertDialog.Builder类为非静态,则在测试期间不会发生此问题。但事实并非如此。有谁知道如何解决此问题?

1 个答案:

答案 0 :(得分:0)

之所以会这样,是因为您必须为Activity设置一个影子类。
实际的Activity类扩展了ContextThemeWrapper类,扩展了ContextWrapper类,扩展了抽象的Context类。

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