I am writing my first larger Groovy application and I want to define me a couple of unit-tests but can't get these to execute. I keep getting an exception java.lang.NoSuchFieldError: NOOP
. I boiled this down to a really simple example which looks like so:
我正在编写我的第一个较大的Groovy应用程序,我想定义几个单元测试,但无法执行这些测试。我一直收到异常java.lang.NoSuchFieldError:NOOP。我把它归结为一个非常简单的例子,看起来是这样的:
package example_project;
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import groovy.test.GroovyTestCase
public class FirstUnitTest extends GroovyTestCase {
@Test
void testJunitTestExecution() {
Assertions.fail("Heureka - this test got executed!")
}
}
This "unit-test" should be executable within Eclipse or via Maven.
这个“单元测试”应该可以在Eclipse中或通过Maven执行。
When I execute that inside Eclipse (using "Run as... --> JUnit Test" using JUnit 5 test runner) I get the exception
当我在Eclipse中执行时(使用“运行为... --> JUnit Test”using JUnit 5 test runner)我得到异常
java.lang.NoSuchFieldError: NOOP
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:55)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:102)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
When executed via Maven (using "mvn test") I get
当通过Maven(使用“MVN test”)执行时,我得到
[INFO] --- surefire:3.1.2:test (default-test) @ FirstUnitTest ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[ERROR] NOOP
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.342 s
[INFO] Finished at: 2023-09-09T16:18:10+02:00
[INFO] ------------------------------------------------------------------------
So - apparently the same error, even though there is no stack trace.
所以-显然是相同的错误,即使没有堆栈跟踪。
My pom.xml:
我的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example_project</groupId>
<artifactId>example_project</artifactId>
<name>example_project</name>
<version>1.0.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
<!-- We also specify the file encoding of our source files, to avoid a warning -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<groovy.version>4.0.12</groovy.version>
<junit.version>5.6.2</junit.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.groovy/groovy-all -->
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/groovy</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/groovy</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>3.0.8-01</version>
</dependency>
</dependencies>
</plugin>
... some plugins re. packaging removed for brevity ...
</plugins>
</build>
</project>
I am using Groovy v4.0.12, Eclipse 2023-06 (v4.28.0) with the Groovy Plugin and Maven v3.9.4.
我正在使用带有Groovy插件的Groovy v4.0.12、Eclipse 2023-06(v4.28.0)和Maven v3.9.4。
What am I missing? Why is my test-method not executed?
我遗漏了什么?为什么我的测试方法没有执行?
更多回答
Please show your Mavem pom.xml
请出示您的Mavem pom.xml
Not helping this problem, but have you tried Spock for unit and integration tests? spockframework.org
对这个问题没有帮助,但您尝试过Spock进行单元测试和集成测试吗?Spockframework.org
@johanneslink I added my pom to the question (I removed a few plugin specs re. packaging, but these should have no relevance here).
@johanenslink我在这个问题上添加了我的POM(我删除了一些插件规范。包装,但这些在这里应该没有相关性)。
优秀答案推荐
After all-in-all about a day's worth of fiddling with this BS I gave up and I am now using JUnit 4.
总而言之,花了大约一天的时间摆弄这个BS,我放弃了,现在我正在使用JUnit4。
After switching the TestRunner-setting to JUnit4 in Eclipse the UT immediately found and properly executed. And after adding
在Eclipse中将TestRunner-设置切换为JUnit4后,UT会立即找到并正确执行。并在添加后
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
</plugin>
to my pom.xml it was also properly found and executed via mvn test
.
对于我的pom.xml,它也被正确地找到并通过MVN测试执行。
Unfortunately - I have to say - this matches my former experience with JUnit-5 / Jupiter. I never had a project so far where getting things to work with JUnit 5 was not a hassle, while with JUnit 4 things always were a snap.
Sure, in theory JUnit 5 has a few cool features but IMHO they are simply not worth such issues!
YMMV but I guess I'll simply stick to JUnit 4.
不幸的是--我不得不说--这与我以前使用JUnit-5/Jupiter的经验相符。到目前为止,我从来没有一个项目让JUnit5工作起来不是一件麻烦的事情,而使用JUnit4总是轻而易举的。当然,理论上JUnit5有一些很酷的功能,但我觉得它们根本不值得这样的问题!YMMV,但我想我将简单地坚持使用JUnit4。
更多回答
I think you gave in too fast. Starting with the JUnit/Jupiter starter projects has always worked for me; have you tried those? In your case I assume that the Groovy plug-in must be told to put the classfiles to the correct location or work in the correct Maven phase.
我觉得你让步得太快了。从JUnit/Jupiter Starter项目开始,我一直都很喜欢;你试过了吗?在您的例子中,我假设必须告诉Groovy插件将类文件放到正确的位置,或者在正确的Maven阶段工作。
That may well be, but - as I wrote - I did quite a lot of searching and tried a lot of things, but none had worked. There also were not very many helpful results. Running JUnit5 tests with Groovy does not exactly seem to be a very popular subject.
也许是这样,但正如我写的那样,我做了相当多的搜索,尝试了很多方法,但都没有奏效。也没有太多有益的结果。使用Groovy运行JUnit5测试似乎并不是一个很受欢迎的主题。
我是一名优秀的程序员,十分优秀!