PowerMock尝试模拟静态getConnection会产生illegalStateException:无法转换类

时间:2013-02-07 13:12:52

标签: java junit mockito powermock illegalstateexception

我正在尝试从类JDBCConnectionFactory创建静态getConnection()方法。我使用powerMock + mockito与Junit4一起进行模拟。

尝试解决方案:

  1. 用Google搜索并更改了很多javassist.jar版本。他们都没有工作。
  2. 以下提到的代码有效。但是当我用JDBCConnectionFactory替换Another.class时,它开始给出下面提到的异常。
  3. 当我删除PrepareFortest注释并尝试运行此JUNIT时,它开始执行getConnection方法本身(我想要模拟)。

    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());
    
       }
    
    }
    
  4. 我收到以下异常:

    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.<init>(JUnit4TestSuiteChunkerImpl.java:59)
        at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
        at org.powermock.modules.junit4.PowerMockRunner.<init>(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.<init>(JUnit4TestReference.java:33)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(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
    

    希望以上信息有所帮助。任何想法/方法/建议都是 欢迎。感谢

2 个答案:

答案 0 :(得分:2)

有时您需要使用doReturn()语法。您是否尝试过以下JDBCConnectionFactory?我将使用您的AnotherClass示例:

@RunWith(PowerMockRunner.class)
@PrepareForTest(AnotherClass.class)
public class MockitoTester {

  @Test
  public void myTest() throws Exception {
    mockStatic(AnotherClass.class);
    PowerMockito.doReturn("mock static string ").when(AnotherClass.class, "getStaticString");
    System.out.println(AnotherClass.getStaticString());
  }
}

另请参阅https://www.javadoc.io/doc/org.powermock/powermock-api-mockito/1.5

上的PowerMockitoStubber

答案 1 :(得分:0)

请查看ArrayIndexOutOfBoundsException when running PowerMock test,我通过这种方式解决了,更新了javassist.jar版本