I am new to JUnit testing and have run into an issue that seems to be caused when I add the junit-platform-suite and junit-platform-suite-engine dependencies to my build file (pom.xml).
I am trying to create a test suite; however, when I add these dependencies, I am no longer able to run my test cases or test classes and the IDE returns that "No tests were found".
Below is my pom.xml file with the JUnit suite dependencies commented out. If I add one or both dependencies, my test cases/classes no longer work.
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
<!-- <dependency>-->
<!-- <groupId>org.junit.platform</groupId>-->
<!-- <artifactId>junit-platform-suite</artifactId>-->
<!-- <version>1.9.3</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite-engine -->
<!-- <dependency>-->
<!-- <groupId>org.junit.platform</groupId>-->
<!-- <artifactId>junit-platform-suite-engine</artifactId>-->
<!-- <version>1.9.3</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
I have tried to invalidate caches and reload, rebuild the module/project, clearing the config settings for JUnit, made sure the Sources Root and Test Sources Root were marked on the correct directory, etc. but nothing seems to work.
To simply run tests written with JUnit 5, you do not need the …Suite… tools. Just specify the aggregate JUnit + Jupiter archetype.
<artifactId>junit-jupiter</artifactId>
If you want to use the Suite features of JUnit, you need to add two more dependencies. Of course, you need at least one test engine such as Jupiter.
The JUnit Platform Suite Engine is for running tests with any of many possible test engines, and no one engine in particular.
JUnit 5 is designed to be a harness for one or more test engines.
One test engine is provided by the JUnit team, named Jupiter. JUnit 5 can run Jupiter-based tests, and JUnit 5 can run tests from other third-party test engines. Jupiter is but one of several test engines compatible with JUnit 5.
If you just want to run Jupiter tests, the replacement for JUnit 4 tests, you do not need JUnit Platform Suite Engine. Instead, just configure your Maven POM to specify the aggregate JUnit + Jupiter archetype.
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0-RC2</version>
<scope>test</scope>
</dependency>
That archetype brings in three artifacts:
These in turn bring in artifacts for the JUnit 5 test harness platform.
Here is an example POM, my modified version of the POM from the Maven Quickstart Archetype.
<?xml version="1.0" encoding="UTF-8"?>
<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>work.basil.example</groupId>
<artifactId>ExJunitJupiter</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ExJunitJupiter</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>20</maven.compiler.release>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0-RC2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M3</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.4.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Here is an example test, modified code taken from the Maven Quickstart Archetype.
package work.basil.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue ( )
{
assertTrue ( true );
}
}
If you want to use the Suite features of JUnit 5, you need to add two more dependencies.
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite-api -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<version>1.10.0-RC2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite-engine -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<version>1.10.0-RC2</version>
<scope>test</scope>
</dependency>
These resolve the imports in code such as my modified version of this example taken from the JUnit 5 documentation.
package work.basil.example;
import org.junit.platform.suite.api.IncludeClassNamePatterns;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
import org.junit.platform.suite.api.SuiteDisplayName;
@Suite
@SuiteDisplayName ( "JUnit Platform Suite Demo" )
@SelectPackages ( "example" )
@IncludeClassNamePatterns ( ".*Tests" )
class SuiteDemo
{
}
See documentation.