You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Groovy运行JUnit 5测试遇NOOP异常,测试无法执行求助

问题:Groovy应用中JUnit 5测试抛出java.lang.NoSuchFieldError: NOOP无法执行

问题场景

开发首个大型Groovy应用时,编写的JUnit 5单元测试无法执行,始终抛出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!")
    }
}

Eclipse执行异常栈

在Eclipse中以JUnit 5运行器执行时,抛出如下异常:

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)

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] ------------------------------------------------------------------------

当前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>

环境信息

  • Groovy v4.0.12
  • Eclipse 2023-06(v4.28.0)带Groovy插件
  • Maven v3.9.4

问题原因与解决办法

1. JUnit版本依赖冲突

JUnit Jupiter 5.6.2对应的JUnit Platform版本是1.6.2,但手动引入了junit-platform-commons:1.7.0,版本不匹配导致NOOP字段找不到(该字段在Platform 1.7+新增,而Jupiter 5.6.2依赖的是1.6.x版本)。

  • 解决:移除手动添加的junit-platform-commons依赖,让Maven自动引入JUnit Jupiter对应的兼容版本。

2. 测试类继承错误

GroovyTestCase是Groovy自带的JUnit 3风格测试基类,和JUnit 5的@Test注解混用会导致执行逻辑混乱,JUnit 5不需要继承任何基类。

  • 解决:删除extends GroovyTestCase;如果需要Groovy的测试特性,可添加groovy-junit5依赖(对应Groovy版本):
<dependency>
    <groupId>org.apache.groovy</groupId>
    <artifactId>groovy-junit5</artifactId>
    <version>${groovy.version}</version>
    <scope>test</scope>
</dependency>

3. Groovy编译器版本不兼容

项目使用Groovy 4.0.12,但Groovy Eclipse Compiler和Batch版本都是3.x,版本过低导致编译运行兼容问题。

  • 解决:升级Groovy Eclipse插件依赖到适配4.x的版本:
<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>4.0.12-01</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-batch</artifactId>
            <version>4.0.12-01</version>
        </dependency>
    </dependencies>
</plugin>

4. 显式配置Surefire插件

建议显式配置Surefire插件以确保JUnit 5测试正确执行:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.2</version>
    <configuration>
        <providerSelection>junit-platform</providerSelection>
    </configuration>
</plugin>

修改后的测试类

package example_project;

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

public class FirstUnitTest {

    @Test
    void testJunitTestExecution() {
        Assertions.fail("Heureka - this test got executed!")
    }
}

内容的提问来源于stack exchange,提问作者mmo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 04:34:58