Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Tycho/How Tos/JUnit5
Creating a new JUnit5 Test Plugin
1.) Create a new Plugin Project (e.g. "foo.bar.tests"). Add a pom.xml file to the root of your project. You do not need to do that if your project is set up for a pom-less build. If your other bundles are already using Tycho you might also want to reference a parent pom. Make sure that the parent pom does use Tycho version 1.2 or above and that the Eclipse repository your are referencing is Eclipse Oxygen or above (needed to get the Junit5 API libraries). The "foo.bar.tests" pom file would look like:
<?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <artifactId>foo.bar.tests</artifactId> <groupId>foo.bar</groupId> <version>1.0.0-SNAPSHOT</version> <packaging>eclipse-test-plugin</packaging> <properties> <tycho-version>1.2.0</tycho-version> </properties> <repositories> <repository> <id>eclipse-oxygen</id> <layout>p2</layout> <url>http://download.eclipse.org/releases/oxygen</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>tycho-maven-plugin</artifactId> <version>${tycho-version}</version> <extensions>true</extensions> </plugin> </plugins> </build> </project>
2.) In order to use JUnit5 Classes, you have to import the JUnit libraries. Open the MANIFEST.MF file in the MANIFEST Editor and switch to the "Dependencies" Tab. Under "Import Packages" add the following 2 packages:
org.junit (4.12.0) org.junit.jupiter.api (5.0.0)
The MANIFEST.MF file of the "foo.bar.tests" Plugin would look like:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Bar Bundle-SymbolicName: foo.bar.tests Bundle-Version: 1.0.0.qualifier Automatic-Module-Name: foo.bar Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Import-Package: org.junit;version="4.12.0", org.junit.jupiter.api;version="5.0.0"
Note that you must import the packages and could not add the JUnit5 dependencies as required bundle dependency. The JUnit4 dependency is needed if you want to run JUnit4 and JUnit5 tests.
3.) Create a new Test class, e.g. "FooTest.java":
package foo.bar; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class FooTest { @Test public void junit5Test() { Assertions.assertTrue(true); } }
4.) Build and Run the tests by calling
mvn clean verify
in the Test Plugin directory and you should get results like:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.011 sec - in foo.bar.FooTest junit5Test Time elapsed: 0.007 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] All tests passed!
Using JUnit5 in existing JUnit4 Test Plugins
Tycho Surefire does include the junit-vintage-engine and so you could mix JUnit4 and JUnit5 Tests in the same Test Plugin. In order to use JUnit5 in an existing JUnit4 Test Plugin, import the org.junit.jupiter.api package in the Test Plugins MANIFEST.MF (Do not remove any JUnit4 dependency). Original :
Require-Bundle: org.junit;bundle-version="4.12.0"
With JUnit5 import:
Import-Package: org.junit.jupiter.api;version="5.0.0" Require-Bundle: org.junit;bundle-version="4.12.0"
Note: When using new JUnit5 features like for example the assertion grouping with assertAll(), you need to import additional packages to make the Tycho build succeed. To use the assertAll() feature the following imports need to be specified:
Import-Package: org.junit.jupiter.api;version="5.5.1", org.junit.jupiter.api.function;version="5.5.1", org.opentest4j;version="1.2.0"
Make sure that you do use Tycho version 1.2 or above and that your referenced Eclipse p2 repository contains the JUnit5 libraries (e.g. Eclipse Oxygen or above):
<repositories> <repository> <id>eclipse-oxygen</id> <layout>p2</layout> <url>http://download.eclipse.org/releases/oxygen</url> </repository> </repositories>