running test suite is not working in junit5 through maven

Deadly 提交于 2020-06-24 16:42:13

问题


@SelectPackages and @SelectClasses tags are not getting parsed with maven test command.Though it is working fine in IDE. Even I tried with tag inside pom.xml.

Here is the code snippet :


PaymentServiceTest.java

package xyz.howtoprogram.junit5.payment;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
public class PaymentServiceTest {
  @Test
  public void doPaymentZeroAmount() {
    assertEquals(1, 1);
  }
}

UserFeatureSuiteTest.java

@RunWith(JUnitPlatform.class)
@SelectPackages("xyz.howtoprogram.junit5.payment")
public class UserFeatureSuiteTest {
}

It is not running any of the test cases under the package. Though there is one test case underneath it.

xyz.howtoprogram.junit5.payment -> PaymentServiceTest.java

Running xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest.

Even I tried with changing the pom.xml like adding the 'include' tag.


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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
    <junit.vintage.version>4.12.0-M2</junit.vintage.version>
    <junit.platform.version>1.0.0-M2</junit.platform.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <includes>
                    <include>**/UserFeatureSuiteTest.java</include>
                </includes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit.jupiter.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                    <version>${junit.vintage.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<dependencies>



    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
    </dependency>
</dependencies>


回答1:


Sorry for the confusion ... I just took a closer look at your code and realized you're trying to mix two concepts.

Your test code is annotated with the JUnit 5 @Test. Therefore this code must be run with the junit-jupiter-engine. That engine does NOT support declarative suites but does read in the configuration from the maven-surefire-plugin.

@RunWith is a JUnit4 annotation and is completely ignored within the junit-jupiter-engine. I don't think it will cause the test to fail but it also will not enable any JUnit 5 tests. The @SelectPackages annotation you've placed on your suite's class will, in this context, only help decide which JUnit 4 tests are run - but you don't have any.




回答2:


@RunWith(JUnitPlatform.class) is not supported when running tests via the JUnit Platform. If the new provider would pickup both UserFeatureSuiteTest and PaymentServiceTest your tests would be executed twice.

From the User Guide:

Annotating a class with @RunWith(JUnitPlatform.class) allows it to be run with IDEs and build systems that support JUnit 4 but do not yet support the JUnit Platform directly.

Thus, if you want UserFeatureSuiteTest to be run by Maven Surefire, you can use the "old" JUnit 4 support that is detected by default, i.e. just remove junit-platform-surefire-provider from the plug-in's dependencies.

Alternatively, you can directly execute your tests, e.g. PaymentServiceTest, through the JUnit Platform with your existing configuration.




回答3:


Below is the final POM,it is working now.

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.howtoprogram</groupId>
<artifactId>junit5-test-suite-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>


<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
    <junit.vintage.version>4.12.0-M2</junit.vintage.version>
    <junit.platform.version>1.0.0-M2</junit.platform.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <includes>
                    <include>**/*SuiteTest.java</include>
                </includes>
            </configuration>

        </plugin>
    </plugins>
</build>

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

so the output is :

     T E S T S
-------------------------------------------------------
Feb 23, 2017 10:21:06 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
INFO: Discovered TestEngines with IDs: [junit-jupiter]
Running xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest
Feb 23, 2017 10:21:06 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
INFO: Discovered TestEngines with IDs: [junit-jupiter]
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec - in xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest



回答4:


By default Maven uses the following naming conventions when looking for tests to run:

  • Test*
  • *Test
  • *TestCase

Your test class doesn't follow these conventions. You should rename it or configure Maven Surefire Plugin to use another pattern for test classes.

Another thing that could lead to your Maven to not work is that all the tests are supposed to be in the following folder:

/my_application/src/test/java/MyFirstExampleTest.java

Here you can see a quite good question for a generalization of your problem, from where I took some parts of my answer. You should take a look at it.

EDIT

Here you can see an example that explain how your pom.xml should be:

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.codefx.demo</groupId>
    <artifactId>junit-5</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0-M3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgs>
                        <arg>-parameters</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0-M3</version>
                    </dependency>
                    <dependency>
                        <!-- contains the engine that actually runs the Jupiter-tests -->
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.0-M3</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

As you can see in this configuration file you specify:

  • Dependency: you need a test scoped dependency of JUnit in order to run tests
  • In the build section you will add the surefire plugin that will run your tests along with its dependencies


来源:https://stackoverflow.com/questions/42392512/running-test-suite-is-not-working-in-junit5-through-maven

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!