我正在使用PowerMock在Gradle任务中验证对JavaCC的静态调用。
在我的@Before注释方法中(我使用JUnit4和Mockito,BTW),我有这个:
Project project = ProjectBuilder.builder().build();
当我在这个用
注释的类中运行任何测试用例时@RunWith(PowerMockRunner.class)
我得到以下内容:
java.lang.NoClassDefFoundError: Could not initialize class org.gradle.testfixtures.internal.ProjectBuilderImpl
at org.gradle.testfixtures.ProjectBuilder.<init>(ProjectBuilder.java:46)
at org.gradle.testfixtures.ProjectBuilder.builder(ProjectBuilder.java:54)
at ca.coglinc.gradle.plugins.javacc.CompileJavaccTaskTest.setUp(CompileJavaccTaskTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:132)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:95)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
我认为我在测试类路径上缺少Gradle运行时配置,但找不到如何解决它。删除PowerMockRunner,我的测试运行正常,但是我无法进行依赖于PowerMock的测试(我知道在不需要PowerMock的情况下应该可测试的东西,但我不是在编写JavaCC而仍想验证用一些参数调用它)。有什么想法吗?
答案 0 :(得分:2)
这answer就是现场!我无法使PowerMockRunner与PojectBuilderImpl一起工作,但至少现在我知道为什么。我试图压制静态初始化器,但随后各种各样的事情都被打破了。我最终放弃了对代码的这一小部分进行单元测试的想法,而是依赖于我的验收测试。
请注意Opal的答案有效,但我不想将useTestNG()添加到我的构建文件中,因为我实际上并没有使用它。这对我来说太违反直觉了。
答案 1 :(得分:1)
您需要修改&lt; ROOT&gt; /plugin/build.gradle ,更改:
test {
maxParallelForks = 10
forkEvery = 50
}
为:
test {
useTestNG()
maxParallelForks = 10
forkEvery = 50
}
似乎应该使用TestNG
来运行测试。