title: AlertDialog unit testing - java.lang.VerifyError: Bad type on operand stack
body: <p>I have an activity <code>MyActivity.java</code> in which I am generating an <code>AlertDialog</code> using <code>AlertDialog.Builder(this);</code>.<br>
  Now I am trying to write a unit test (<code>junit</code>) for <code>MyActivity.java</code> by treating is as a simple java class and creating shadow classes for Android's framework classes.
  When I try to run the test, I get an error while trying to instantiate the class : <code>java.lang.VerifyError: Bad type on operand stack</code></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>From what I researched, this error is happening because I am trying to reference <code>this</code> in a static context (<code>AlertDialog.Builder</code>). So basically I am trying to reference <code>MyActivity</code> before it has been fully constructed.
  <br><br>
  I figured if in my <code>AlertDialog</code>'s shadow class I made <code>AlertDialog.Builder</code> class non-static, the problem would not occur during test. But that is not the case. Does anyone have any clue how can I get a workaround for this ?</p>