我正在使用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,仍然想验证它是否用一些参数调用)。有什么想法吗?
这个答案很准确!我没能让PowerMockRunner与PojectBuilderImpl一起工作,但至少现在我知道为什么了。我试着抑制静态初始值设定项,但后来各种东西都坏了。我最终放弃了对代码的这一小部分进行单元测试的想法,而是依靠我的验收测试。
请注意,Opal的答案是有效的,但我不想将useTestNG()添加到我的构建文件中,因为我实际上并没有使用它。这对我来说似乎太违反直觉了。
您需要修改<ROOT>plugin/build.gradle,更改:
test {
maxParallelForks = 10
forkEvery = 50
}
至:
test {
useTestNG()
maxParallelForks = 10
forkEvery = 50
}
似乎应该使用TestNG
来运行测试。