通过JUnit Core API运行PowerMock测试时出现问题

时间:2019-01-15 17:52:14

标签: java junit mockito powermock

尝试从我的代码中使用JUnit运行PowerMock测试时遇到麻烦。这是我加载和运行测试类的方式:

        URL[] urls = getTargetClassesUrls();

        try (URLClassLoader cl = new URLClassLoader(urls, Test.class.getClassLoader())) {

            Class clazz = cl.loadClass(className);

            return JUnitCore.runClasses(clazz);

这是我希望JUnit运行的类:

@PrepareForTest(System.class)
public class MyTest {

  static {PowerMockAgent.initializeIfNeeded();}

  @Rule
  public PowerMockRule rule = new PowerMockRule();

  @Test
  public void test() {
    PowerMockito.mockStatic(System.class);
    Mockito.when(System.currentTimeMillis()).thenReturn((long)123);
    System.out.println(System.currentTimeMillis());
  }
}

最初,我尝试使用@RunWith(PowerMockRunner.class)运行此类。当跑步者尝试加载我的测试课程时,这会带来问题。 PowerMock文档建议bootstrapping using a Java agent有关类加载问题。

如果我使用Eclipse工具执行测试,JUnit将成功运行测试。 这是我得到的故障跟踪:

    java.lang.VerifyError: Expecting a stackmap frame at branch target 89
Exception Details:
  Location:
    ar/com/cnet/junit/JUnitArchiver.saveTestSuiteWithoutErrors(Lorg/junit/runner/Result;Ljava/lang/String;Ljava/lang/String;Lar/com/cnet/Config;)Z @11: ifeq
  Reason:
    Expected stackmap frame at this location.
  Bytecode:
    0x0000000: 2b2c b800 2d3a 042a b600 3399 004e bb00
    0x0000010: 3559 1904 b700 363a 05bb 0035 59bb 0038
    0x0000020: 592d b600 3db8 0043 b700 44b2 0048 b600
    0x0000030: 4c19 042d b600 4fb6 0053 b600 57b6 004c
    0x0000040: b600 5ab7 0036 3a06 1905 b600 5d99 000c
    0x0000050: 1905 1906 b800 6304 acb2 0017 bb00 3859
    0x0000060: 1265 b700 442c b600 4c12 67b6 004c b600
    0x0000070: 5ab9 006c 0200 2ab6 0070 b900 7601 003a
    0x0000080: 06a7 001c 1906 b900 7c01 00c0 007e 3a05
    0x0000090: b200 1719 05b6 0081 b900 6c02 0019 06b9
    0x00000a0: 0084 0100 9aff e003 ac                 

    at ar.com.cnet.multithreading.WorkerThread.run(WorkerThread.java:63)
    at ar.com.cnet.multithreading.WorkerThreadTest.testRunThreadWithMocks(WorkerThreadTest.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    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)

谢谢。

0 个答案:

没有答案