JUnit 5 Surefire Maven: how to run tests for a dynamic web module project?

时间:2017-12-17 08:21:09

标签: java maven junit

The "mvn test" compiles my test case, but it does not run it:

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test-server ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to my-server\backend\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.20.1:test (default-test) @ my-server ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]

pom.xml properties:

<packaging>war</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    <java.version>1.8</java.version>
    <junit.version>4.12</junit.version>
    <junit.jupiter.version>5.0.0</junit.jupiter.version>
    <junit.vintage.version>${junit.version}.0</junit.vintage.version>
    <junit.jupiter.version>5.0.0</junit.jupiter.version>
    <junit.platform.version>1.0.0</junit.platform.version>      
</properties>

Build settings:

<build>
    <finalName>my-server</finalName>
    <testSourceDirectory>src/test</testSourceDirectory>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Dependencies:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>${junit.vintage.version}</version>
        <scope>test</scope>
    </dependency>

The test class is in the testSourceDirectory directory, its name begins with "Test", and it has the @Test annotation.

I suspect that the tests are not executed because the packaging is "war" instead of "jar". But I cannot change that - there is a single "packaging" setting for the whole project, and it must be "war" because it is really a web app.

So how do run my tests, and keep the "war" packaging at the same time?

UPDATE After adding the suggessted surefire provider, the test fails. surefire-reports/*.dump file shows this traceback:

# Created on 2017-12-17T10:58:50.171
java.lang.NoSuchMethodError: org.apache.maven.surefire.report.RunListener.testSetStarting(Lorg/apache/maven/surefire/report/ReportEntry;)V
    at org.junit.platform.surefire.provider.JUnitPlatformProvider.invokeSingleClass(JUnitPlatformProvider.java:137)
    at org.junit.platform.surefire.provider.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:126)
    at org.junit.platform.surefire.provider.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:105)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)

2 个答案:

答案 0 :(得分:2)

JUnit 5 will works fine with following combination of dependencies and plugin version, both in JDK 8/11. 

Dependency
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.2.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.2.0</version>
    <scope>test</scope>
</dependency>

Build
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
</plugin>

答案 1 :(得分:1)

似乎是2.21.0中org.apache.maven.surefire.report.RunListener.testSetStarting was changed的签名。 junit-platform-surefire-provider依赖于surefire-api.2.19.1,因此它调用testSetStarting,其实例为ReportEntry,方法现在接受TestSetReportEntry

依赖关系为upgraded to 2.21.0,因此我们只需等待junit 5的下一个版本。

我已经建立了junit的当前主分支,并且可以确认junit-platform-1.2.0-SNAPSHOT和juniper-5.2.0-SNAPSHOT按预期工作。