Groovy:如何运行 JUnit 测试(使用 JUnit 5)?

问题描述 投票:0回答:1

我正在编写第一个较大的 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!")
    }
}

这个“单元测试”应该可以在 Eclipse 中或通过 Maven 执行。

当我在 Eclipse 中执行该操作时(使用 JUnit 5 测试运行程序使用“Run as... --> JUnit Test”),我得到了异常

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。

我错过了什么?为什么我的测试方法没有执行?

groovy junit5
1个回答
0
投票

总而言之,在花了一天时间摆弄这个废话之后,我放弃了,现在使用 JUnit 4。

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

我的 pom.xml 也可以通过 maven 正确找到并执行。

不幸的是 - 我不得不说 - 这与我之前使用 JUnit-5 / Jupiter 的经验相符。到目前为止,我从来没有有过一个项目,其中使用 JUnit 5 来工作不是很麻烦,而且是 PITA。 而使用 JUnit 4 事情总是轻而易举。 YMMV 但我想我会坚持使用 JUnit 4。 当然,理论上 JUnit 5 有一些很酷的功能,但恕我直言,它们根本不值得这样的问题!

© www.soinside.com 2019 - 2024. All rights reserved.