在Powermockito中使用@PrepareForTest时出错

时间:2019-08-20 00:01:59

标签: mocking mockito powermockito

这是pom.xml中的依赖项:

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.24.0-GA</version>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>

当我在测试中包含@PrepareForTest时,出现此错误:

[ERROR] java.lang.StackOverflowError
[ERROR] at   javassist.bytecode.stackmap.Tracer.checkParamTypes(Tracer.java:927)
[ERROR] at javassist.bytecode.stackmap.Tracer.checkParamTypes(Tracer.java:918)
[ERROR] at javassist.bytecode.stackmap.Tracer.checkParamTypes(Tracer.java:918)
[ERROR] at javassist.bytecode.stackmap.Tracer.checkParamTypes(Tracer.java:918)
[ERROR] at javassist.bytecode.stackmap.Tracer.checkParamTypes(Tracer.java:918)
[ERROR] at javassist.bytecode.stackmap.Tracer.doInvokeMethod(Tracer.java:798)
[ERROR] at javassist.bytecode.stackmap.Tracer.doOpcode148_201(Tracer.java:594)
[ERROR] at javassist.bytecode.stackmap.Tracer.doOpcode(Tracer.java:78)
[ERROR] at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:195)
[ERROR] at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:207)
[ERROR] at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:207)
[ERROR] at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:207)
[ERROR] at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:207)
[ERROR] at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:207)

我试图更改javassist版本,但没有帮助。

编辑:添加出现上述错误的代码。

@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class ServiceTest
{
@Test
public void test1() {
PowerMockito.mockStatic(A.class);
Mockito.when(A.findNumber()).thenReturn(13);
System.out.println(A.findNumber());
}

@Test
public void test2() {
    PowerMockito.mockStatic(A.class);
    Mockito.when(A.getNumber(25)).thenReturn(50);
    System.out.println(A.getNumber(25));
}
}

以前有没有人看过这个问题?

0 个答案:

没有答案