3

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

尝试的解决方案:

  1. 谷歌搜索并更改了很多 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());
    
       }
    
    }
    

我收到以下异常:

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

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

4

2 回答 2

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

于 2013-02-11T16:15:12.143 回答
0

运行PowerMock测试时请看ArrayIndexOutOfBoundsException ,我是这样解决的,更新javassist.jar版本

于 2013-03-27T13:06:38.607 回答