title: PowerMock trying to mock static getConnection gives illegalStateException: Failed to transform class
body: <p>I am trying to make static getConnection() method from a class JDBCConnectionFactory. I am using powerMock + mockito for mocking along with Junit4. </p>
  
  <h3>Solutions tried :</h3>
  
  <ol>
  <li>Googled for it and changed a lot of javassist.jar versions. None of them worked. <br/></li>
  <li>The below mentioned code works. But when I replace Another.class with JDBCConnectionFactory, it starts giving the below mentioned exception.<br/></li>
  <li><p>When I remove PrepareFortest annotation and try to run this JUNIT, it starts executing the getConnection method itself(which I want to mock).</p>
  
  <pre><code>import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.Mockito;
  import org.powermock.core.classloader.annotations.PrepareForTest;
  import org.powermock.modules.junit4.PowerMockRunner;
  
  @RunWith(PowerMockRunner.class)
  @PrepareForTest(AnotherClass.class)
  public class MockitoTester {
  
     @Test
     public void myTest() throws Exception {
  
        mockStatic(AnotherClass.class);
        Mockito.when(AnotherClass.getStaticString()).thenReturn("mock static string ");
        System.out.println(AnotherClass.getStaticString());
  
     }
  
  }
  </code></pre></li>
  </ol>
  
  <p>I am getting the below exception:</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to transform class with name org.onesync.dq.gui.testng.testNG.MockitoTester. Reason: 3
      at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:207)
      at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:145)
      at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
      at java.lang.Class.forName0(Native Method) 
      at java.lang.Class.forName(Class.java:247)
      at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:133)
      at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:39)
      at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:217)
      at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.&lt;init&gt;(JUnit4TestSuiteChunkerImpl.java:59)
      at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.&lt;init&gt;(AbstractCommonPowerMockRunner.java:32)
      at org.powermock.modules.junit4.PowerMockRunner.&lt;init&gt;(PowerMockRunner.java:31)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
      at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
      at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
      at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
      at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
      at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.&lt;init&gt;(JUnit4TestReference.java:33)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.&lt;init&gt;(JUnit4TestClassReference.java:25)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  Caused by: java.lang.ArrayIndexOutOfBoundsException: 3
      at javassist.bytecode.ByteArray.write16bit(ByteArray.java:40)
      at javassist.bytecode.StackMapTable$Shifter.update(StackMapTable.java:744)
      at javassist.bytecode.StackMapTable$Shifter.sameFrame(StackMapTable.java:720)
      at javassist.bytecode.StackMapTable$Walker.stackMapFrames(StackMapTable.java:194)
      at javassist.bytecode.StackMapTable$Walker.parse(StackMapTable.java:179)
      at javassist.bytecode.StackMapTable$Shifter.doit(StackMapTable.java:714)
      at javassist.bytecode.StackMapTable.shiftPc(StackMapTable.java:693)
      at javassist.bytecode.CodeIterator.insertGap0(CodeIterator.java:676)
      at javassist.bytecode.CodeIterator.insertGap(CodeIterator.java:636)
      at javassist.bytecode.CodeIterator.insertGapCore(CodeIterator.java:467)
      at javassist.bytecode.CodeIterator.insertGap(CodeIterator.java:413)
      at javassist.expr.Expr.replace0(Expr.java:298)
      at javassist.expr.FieldAccess.replace(FieldAccess.java:213)
      at org.powermock.core.transformers.impl.MainMockTransformer$PowerMockExpressionEditor.edit(MainMockTransformer.java:276)
      at javassist.expr.ExprEditor.loopBody(ExprEditor.java:197)
      at javassist.expr.ExprEditor.doit(ExprEditor.java:90)
      at javassist.CtClassType.instrument(CtClassType.java:1289)
      at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:75)
      at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:203)
      ... 29 more
  </code></pre>
  
  <p>Hope the above information helps. Any ideas/approaches/suggestions are
  welcome.  Thanks</p>