2

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

Sankarsan Padhy
  • 61
  • 1
  • 1
  • 6

4 Answers4

6

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.

Steve Moyer
  • 5,663
  • 1
  • 24
  • 34
  • I just re-edited the original pom.xml. I have added the surefire dependency to vintage and jupiter ( either one of them or both); It is not working in all cases. You could try out a simple program. – Sankarsan Padhy Feb 23 '17 at 13:00
  • @SankarsanPadhy - no, I'm definitely not going to help you build a tutorial for an "answers site" like http://how-to-program.xyz/. If you want to give expert advice, you need to spend the time to become an expert. – Steve Moyer Feb 23 '17 at 13:24
  • Sorry for the confusion. I am trying to implement junit5 feature in our project. So, I looked around for the documents and example sites to try out them; but stumbled upon this issue. So, desperately seeking an help. I cannot just post my company code here. So posted the example I am trying in my local system. – Sankarsan Padhy Feb 23 '17 at 13:41
  • @SankarsanPadhy - I'd like to apologize for being so critical ... I made some bad assumptions based on your samples above and shouldn't have dismissed your question out-of-hand. – Steve Moyer Feb 23 '17 at 16:57
  • no issues @steve-moyer. I tried and just figured out the solution.Kindly see my latest post. – Sankarsan Padhy Feb 23 '17 at 17:04
  • I removed the \@RunWith and kept just the \@SelectPackages(...) and that works. Many of the jUnit5 tutorials mistakenly recommend using @RunWith for a testing suite which is incorrect. – JesseBoyd Dec 21 '17 at 14:47
4

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
Sankarsan Padhy
  • 61
  • 1
  • 1
  • 6
3

@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.

Marc Philipp
  • 1,848
  • 12
  • 25
1

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
Community
  • 1
  • 1
rakwaht
  • 3,666
  • 3
  • 28
  • 45
  • It did not help. So, I have re-edited the original issue with detailed steps. – Sankarsan Padhy Feb 23 '17 at 05:54
  • @SankarsanPadhy edited my answer explaining you how your pom should be. I think that this may be your problem – rakwaht Feb 23 '17 at 08:30
  • not working. I have reedited the original issue with the complete pom.xml – Sankarsan Padhy Feb 23 '17 at 10:29
  • @SankarsanPadhy Did you try to refactor the name of your test class from TestPaymentService.java to PaymentServiceTest.java. I was looking into http://junit.org/junit5/docs/current/user-guide/ and I think that, the suits is going to recognize only class that match with this ^.*Tests?$ – rakwaht Feb 23 '17 at 10:43
  • I refactored it, did not update the original issue. it is not working. I would strongly request you to run a small program yourself. I tried with several trial and error but could not succeed till yet. – Sankarsan Padhy Feb 23 '17 at 11:25
  • This answer helped me. Thanks – Sebastian D'Agostino Oct 13 '18 at 16:21