Junit5: Test method not be executed

Created on 28 Aug 2019  路  3Comments  路  Source: junit-team/junit5

I have a question, the test method name does not starts with test can not be executed.
IDEA run the testcase OK.
but the test case was skipped when I use mvn test -B to run the testcase .

junit4 does not have this problem.

Steps to reproduce

public class DemoTest {
    @Test
    public void helloWorldTest() {
        System.out.println("hello");
    }
}

Context

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>9</version>
    </parent>

    <groupId>com.bestvike</groupId>
    <artifactId>linq-perf</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>linq-perf</name>
    <description>Benchmark for LINQ to Objects (Java)</description>
    <url>https://github.com/timandy/linq-perf</url>

    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <scm>
        <url>https://github.com/timandy/linq</url>
        <connection>scm:git:https://github.com/timandy/linq-perf.git</connection>
        <developerConnection>scm:git:https://github.com/timandy/linq-perf.git</developerConnection>
    </scm>

    <developers>
        <developer>
            <name>TimAndy</name>
            <email>[email protected]</email>
            <url>https://github.com/timandy</url>
        </developer>
    </developers>

    <dependencies>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <version>1.21</version>
        </dependency>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-generator-annprocess</artifactId>
            <version>1.21</version>
        </dependency>

        <dependency>
            <groupId>com.bestvike</groupId>
            <artifactId>linq</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.5.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>linq-perf</finalName>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>org.openjdk.jmh.Main</mainClass>
                                </transformer>
                            </transformers>
                            <filters>
                                <filter>
                                    <!--
                                        Shading signed JARs will fail without this.
                                        http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
                                    -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Maven Surefire question

All 3 comments

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. We prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

Having said that, if you want people to be able to help you diagnose this issue on Stack Overflow, you'll need to provide the imports used in your test class as well as the resolved dependency tree for Maven (which would show what testing libraries you have in the classpath).

Also, based on your claim that only methods matching the pattern test*() are executed, if you are using the @Test annotation from JUnit Jupiter, I would assume you don't have the junit-jupiter-engine on the classpath. In other words, it may be that your tests are being executed as POJO Tests by Maven (see https://stackoverflow.com/a/51292319/388980).

I just noticed that you have a dependency on the junit-jupiter BOM. So that should properly pull in the junit-jupiter-engine.

In which case, you're probably just missing the configuration for Maven Surefire.

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>

See also: https://github.com/junit-team/junit5-samples/tree/r5.5.1/junit5-jupiter-starter-maven

@sbrannen Thanks a lot. It works.

Was this page helpful?
0 / 5 - 0 ratings