Groovy:如何运行JUnit-Test(使用JUnit 5)?

i1icjdpr  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(83)

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

当通过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 Plugin和Maven v3.9.4。
我错过了什么?为什么我的测试方法没有被执行?

n9vozmp4

n9vozmp41#

在花了一天的时间摆弄这个BS之后,我放弃了,现在我使用JUnit 4。
在Eclipse中将TestRunner设置切换到JUnit 4后,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,它也被正确地找到并通过mvn test执行。
不幸的是--我不得不说--这与我以前使用JUnit-5 / Jupiter的经验相匹配。到目前为止,我 * 从来没有 * 过一个项目,让事情与JUnit 5一起工作 * 不是 * 一个麻烦,而与JUnit 4一起工作总是很容易。当然,理论上JUnit 5有一些很酷的特性,但恕我直言,它们根本不值得这样的问题!YMMV,但我想我会简单地坚持JUnit 4。

相关问题