4

将 powermock 版本从 1X 升级到 2X 后,对 System 类的 verifystatic 的引用失败

以下是我当前的配置 mockito-core - 2.27.0 powermock-api-mockito2 - 2.0.2 powermock-api-support - 2.0.2 powermock-module-junit4 - 2.0.2

下面的代码在 powermock 1X 上运行良好

@RunWith(PowerMockRunner.class)
@PrepareForTest({ System.class })
public class SystemMockStaticTest {
  @Test
  public void testSystem() {
    PowerMockito.mockStatic(System.class);
    Properties properties = System.getProperties();
    PowerMockito.verifyStatic();
    System.getProperties();
  }
}

我进行了以下更改以使其与 powermock2.0.2 一起使用

    PowerMockito.mockStatic(System.class);
    Properties properties = System.getProperties();
    PowerMockito.verifyStatic(System.class);
    System.getProperties();

在运行测试时低于异常。

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class java.lang.System
Mockito cannot mock/spy because :
 - final class
    at SystemMockStaticTest.testSystem(SystemMockStaticTest.java:19)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

请帮助找到此异常的修复程序。

4

0 回答 0