gpt4 book ai didi

java - 将 Gradle 测试装置与 PowerMock 结合使用

转载 作者:行者123 更新时间:2023-12-01 22:40:58 24 4
gpt4 key购买 nike

我正在使用 PowerMock 来验证 Gradle 任务中对 JavaCC 的静态调用。

在我的 @Before 带注释的方法中(我正在使用 JUnit4 和 Mockito,顺便说一句),我有这个:

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,并且仍然想验证它是否被某些调用参数)。有什么想法吗?

最佳答案

这个answer非常准确!我无法使 PowerMockRunner 与 PojectBuilderImpl 一起工作,但至少现在我知道原因了。我尝试抑制静态初始值设定项,但随后各种事情都被破坏了。我最终放弃了对代码的这一小部分进行单元测试的想法,而是依赖于我的验收测试。

请注意,Opal 的答案有效,但我不想将 useTestNG() 添加到我的构建文件中,因为我实际上并没有使用它。这对我来说似乎太违反直觉了。

关于java - 将 Gradle 测试装置与 PowerMock 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26147042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com