This is a living collection of topics/snippets that come up while migrating to / using JUnit5 …
Temporary Folders
What is the equivalent of ExternalResource and TemporaryFolder in JUnit 5?
In a Maven project you might want to add the migration support with:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>5.0.2</version>
</dependency>
Expected Exceptions
In JUnit 4 you would have used the @Test
annotation’s field expected
like this:
@Test(expected = IndexOutOfBoundsException.class)
public void empty() {
new ArrayList<Object>().get(0);
}
With Java 8 / JUnit 5 this field is no longer available.
It has be superseded by Assertions.assertThrows()
.
@Test
public void empty() {
assertThrows(IndexOutOfBoundsException.class, () -> new ArrayList<>().get(0));
}
Support JUnit 5 test in a Gradle Project
buildscript {
repositories {
mavenCentral()
// The following is only necessary if you want to use SNAPSHOT releases.
// maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
}
}
apply plugin: 'org.junit.platform.gradle.plugin'
Then add the JUnit5 dependencies:
dependencies {
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.2")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.2")
}
Ignored / Disabled tests
The pendant to @Ignore
in JUnit 4 is org.junit.jupiter.api.Disabled
.
Detailed information is available in the section Annotations of the JUnit 5 User Guide
—
Happy Testing…
—
Other useful Junit 5 related resources