JUnit 5 User Guide
JUnit 5 User Guide
Stefan Bechtold, Sam Brannen, Johannes Link, Matthias Merdes, Marc Philipp,
Christian Stein
Version 5.1.0
Table of Contents
1. Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2. Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
3. Writing Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.1. Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.4. Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.5. Assumptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4. Running Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5. Extension Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7. Advanced Topics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
8. API Evolution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
9. Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Translations
This document is also available in Simplified Chinese.
The JUnit Platform serves as a foundation for launching testing frameworks on the JVM. It also
defines the TestEngine API for developing a testing framework that runs on the platform.
Furthermore, the platform provides a Console Launcher to launch the platform from the command
line and build plugins for Gradle and Maven as well as a JUnit 4 based Runner for running any
TestEngine on the platform.
JUnit Jupiter is the combination of the new programming model and extension model for writing
tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine for running Jupiter
based tests on the platform.
JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.
2. Installation
Artifacts for final releases and milestones are deployed to Maven Central.
1
2.1.1. JUnit Platform
• Version: 1.1.0
• Artifact IDs:
junit-platform-commons
Internal common library/utilities of JUnit. These utilities are intended solely for usage within
the JUnit framework itself. Any usage by external parties is not supported. Use at your own
risk!
junit-platform-console
Support for discovering and executing tests on the JUnit Platform from the console. See
Console Launcher for details.
junit-platform-console-standalone
An executable JAR with all dependencies included is provided at Maven Central under the
junit-platform-console-standalone directory. See Console Launcher for details.
junit-platform-engine
Public API for test engines. See Plugging in Your Own Test Engine for details.
junit-platform-gradle-plugin
Support for discovering and executing tests on the JUnit Platform using Gradle.
junit-platform-launcher
Public API for configuring and launching test plans — typically used by IDEs and build tools.
See JUnit Platform Launcher API for details.
junit-platform-runner
Runner for executing tests and test suites on the JUnit Platform in a JUnit 4 environment. See
Using JUnit 4 to run the JUnit Platform for details.
junit-platform-suite-api
Annotations for configuring test suites on the JUnit Platform. Supported by the JUnitPlatform
runner and possibly by third-party TestEngine implementations.
junit-platform-surefire-provider
Support for discovering and executing tests on the JUnit Platform using Maven Surefire.
• Version: 5.1.0
• Artifact IDs:
junit-jupiter-api
JUnit Jupiter API for writing tests and extensions.
2
junit-jupiter-engine
JUnit Jupiter test engine implementation, only required at runtime.
junit-jupiter-params
Support for parameterized tests in JUnit Jupiter.
junit-jupiter-migrationsupport
Migration support from JUnit 4 to JUnit Jupiter, only required for running selected JUnit 4
rules.
• Version: 5.1.0
• Artifact ID:
junit-vintage-engine
JUnit Vintage test engine implementation that allows to run vintage JUnit tests, i.e. tests
written in the JUnit 3 or JUnit 4 style, on the new JUnit Platform.
2.1.4. Dependencies
All of the above artifacts have a dependency in their published Maven POMs on the following @API
Guardian JAR.
• Version: 1.0.0
In addition, most of the above artifacts have a direct or transitive dependency to the following
OpenTest4J JAR.
• Version: 1.0.0
3
org.junit.platform org.apiguardian
junit-platform-gradle-plugin apiguardian-api
org.junit.jupiter org.junit.vintage
org.opentest4j
opentest4j junit-platform-commons
3. Writing Tests
A first test case
import org.junit.jupiter.api.Test;
class FirstJUnit5Tests {
@Test
void myFirstTest() {
assertEquals(2, 1 + 1);
}
3.1. Annotations
JUnit Jupiter supports the following annotations for configuring tests and extending the framework.
All core annotations are located in the org.junit.jupiter.api package in the junit-jupiter-api
module.
4
Annotation Description
@Test Denotes that a method is a test method. Unlike JUnit 4’s @Test annotation, this
annotation does not declare any attributes, since test extensions in JUnit
Jupiter operate based on their own dedicated annotations. Such methods are
inherited unless they are overridden.
@ParameterizedTest Denotes that a method is a parameterized test. Such methods are inherited
unless they are overridden.
@RepeatedTest Denotes that a method is a test template for a repeated test. Such methods are
inherited unless they are overridden.
@TestFactory Denotes that a method is a test factory for dynamic tests. Such methods are
inherited unless they are overridden.
@TestInstance Used to configure the test instance lifecycle for the annotated test class. Such
annotations are inherited.
@TestTemplate Denotes that a method is a template for test cases designed to be invoked
multiple times depending on the number of invocation contexts returned by
the registered providers. Such methods are inherited unless they are
overridden.
@DisplayName Declares a custom display name for the test class or test method. Such
annotations are not inherited.
@BeforeEach Denotes that the annotated method should be executed before each @Test,
@RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class;
analogous to JUnit 4’s @Before. Such methods are inherited unless they are
overridden.
@AfterEach Denotes that the annotated method should be executed after each @Test,
@RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class;
analogous to JUnit 4’s @After. Such methods are inherited unless they are
overridden.
@BeforeAll Denotes that the annotated method should be executed before all @Test,
@RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current
class; analogous to JUnit 4’s @BeforeClass. Such methods are inherited (unless
they are hidden or overridden) and must be static (unless the "per-class" test
instance lifecycle is used).
@AfterAll Denotes that the annotated method should be executed after all @Test,
@RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current
class; analogous to JUnit 4’s @AfterClass. Such methods are inherited (unless
they are hidden or overridden) and must be static (unless the "per-class" test
instance lifecycle is used).
@Nested Denotes that the annotated class is a nested, non-static test class. @BeforeAll
and @AfterAll methods cannot be used directly in a @Nested test class unless
the "per-class" test instance lifecycle is used. Such annotations are not
inherited.
@Tag Used to declare tags for filtering tests, either at the class or method level;
analogous to test groups in TestNG or Categories in JUnit 4. Such annotations
are inherited at the class level but not at the method level.
@Disabled Used to disable a test class or test method; analogous to JUnit 4’s @Ignore. Such
annotations are not inherited.
5
Annotation Description
@ExtendWith Used to register custom extensions. Such annotations are inherited.
JUnit Jupiter annotations can be used as meta-annotations. That means that you can define your
own composed annotation that will automatically inherit the semantics of its meta-annotations.
For example, instead of copying and pasting @Tag("fast") throughout your code base (see Tagging
and Filtering), you can create a custom composed annotation named @Fast as follows. @Fast can then
be used as a drop-in replacement for @Tag("fast").
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Tag;
6
A standard test case
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
class StandardTests {
@BeforeAll
static void initAll() {
}
@BeforeEach
void init() {
}
@Test
void succeedingTest() {
}
@Test
void failingTest() {
fail("a failing test");
}
@Test
@Disabled("for demonstration purposes")
void skippedTest() {
// not executed
}
@AfterEach
void tearDown() {
}
@AfterAll
static void tearDownAll() {
}
7
3.3. Display Names
Test classes and test methods can declare custom display names — with spaces, special characters,
and even emojis — that will be displayed by test runners and test reporting.
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@Test
@DisplayName("Custom test name containing spaces")
void testWithDisplayNameContainingSpaces() {
}
@Test
@DisplayName("╯°□°)╯")
void testWithDisplayNameContainingSpecialCharacters() {
}
@Test
@DisplayName("ὣ")
void testWithDisplayNameContainingEmoji() {
}
3.4. Assertions
JUnit Jupiter comes with many of the assertion methods that JUnit 4 has and adds a few that lend
themselves well to being used with Java 8 lambdas. All JUnit Jupiter assertions are static methods
in the org.junit.jupiter.api.Assertions class.
import org.junit.jupiter.api.Test;
class AssertionsDemo {
8
@Test
void standardAssertions() {
assertEquals(2, 2);
assertEquals(4, 4, "The optional assertion message is now the last parameter.
");
assertTrue('a' < 'b', () -> "Assertion messages can be lazily evaluated -- "
+ "to avoid constructing complex messages unnecessarily.");
}
@Test
void groupedAssertions() {
// In a grouped assertion all assertions are executed, and any
// failures will be reported together.
assertAll("person",
() -> assertEquals("John", person.getFirstName()),
() -> assertEquals("Doe", person.getLastName())
);
}
@Test
void dependentAssertions() {
// Within a code block, if an assertion fails the
// subsequent code in the same block will be skipped.
assertAll("properties",
() -> {
String firstName = person.getFirstName();
assertNotNull(firstName);
@Test
void exceptionTesting() {
9
Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("a message");
});
assertEquals("a message", exception.getMessage());
}
@Test
void timeoutNotExceeded() {
// The following assertion succeeds.
assertTimeout(ofMinutes(2), () -> {
// Perform task that takes less than 2 minutes.
});
}
@Test
void timeoutNotExceededWithResult() {
// The following assertion succeeds, and returns the supplied object.
String actualResult = assertTimeout(ofMinutes(2), () -> {
return "a result";
});
assertEquals("a result", actualResult);
}
@Test
void timeoutNotExceededWithMethod() {
// The following assertion invokes a method reference and returns an object.
String actualGreeting = assertTimeout(ofMinutes(2), AssertionsDemo::greeting);
assertEquals("Hello, World!", actualGreeting);
}
@Test
void timeoutExceeded() {
// The following assertion fails with an error message similar to:
// execution exceeded timeout of 10 ms by 91 ms
assertTimeout(ofMillis(10), () -> {
// Simulate task that takes more than 10 ms.
Thread.sleep(100);
});
}
@Test
void timeoutExceededWithPreemptiveTermination() {
// The following assertion fails with an error message similar to:
// execution timed out after 10 ms
assertTimeoutPreemptively(ofMillis(10), () -> {
// Simulate task that takes more than 10 ms.
Thread.sleep(100);
});
}
10
return "Hello, World!";
}
JUnit Jupiter also comes with a few assertion methods that lend themselves well to being used in
Kotlin. All JUnit Jupiter Kotlin assertions are top-level functions in the org.junit.jupiter.api
package.
11
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.assertThrows
class AssertionsDemoKotlin {
@Test
fun `grouped assertions`() {
assertAll("person",
{ assertEquals("John", person.firstName) },
{ assertEquals("Doe", person.lastName) }
)
}
@Test
fun `exception testing`() {
val exception = assertThrows<IllegalArgumentException> ("Should throw an
exception") {
throw IllegalArgumentException("a message")
}
assertEquals("a message", exception.message)
}
@Test
fun `assertions from a stream`() {
assertAll(
"people with name starting with J",
people
.stream()
.map {
// This mapping returns Stream<() -> Unit>
{ assertTrue(it.firstName.startsWith("J")) }
}
)
}
@Test
fun `assertions from a collection`() {
assertAll(
"people with last name of Doe",
people.map { { assertEquals("Doe", it.lastName) } }
)
}
12
3.4.1. Third-party Assertion Libraries
Even though the assertion facilities provided by JUnit Jupiter are sufficient for many testing
scenarios, there are times when more power and additional functionality such as matchers are
desired or required. In such cases, the JUnit team recommends the use of third-party assertion
libraries such as AssertJ, Hamcrest, Truth, etc. Developers are therefore free to use the assertion
library of their choice.
For example, the combination of matchers and a fluent API can be used to make assertions more
descriptive and readable. However, JUnit Jupiter’s org.junit.jupiter.api.Assertions class does not
provide an assertThat() method like the one found in JUnit 4’s org.junit.Assert class which accepts
a Hamcrest Matcher. Instead, developers are encouraged to use the built-in support for matchers
provided by third-party assertion libraries.
The following example demonstrates how to use the assertThat() support from Hamcrest in a JUnit
Jupiter test. As long as the Hamcrest library has been added to the classpath, you can statically
import methods such as assertThat(), is(), and equalTo() and then use them in tests like in the
assertWithHamcrestMatcher() method below.
import org.junit.jupiter.api.Test;
class HamcrestAssertionDemo {
@Test
void assertWithHamcrestMatcher() {
assertThat(2 + 1, is(equalTo(3)));
}
Naturally, legacy tests based on the JUnit 4 programming model can continue using
org.junit.Assert#assertThat.
3.5. Assumptions
JUnit Jupiter comes with a subset of the assumption methods that JUnit 4 provides and adds a few
that lend themselves well to being used with Java 8 lambdas. All JUnit Jupiter assumptions are static
methods in the org.junit.jupiter.api.Assumptions class.
13
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.jupiter.api.Assumptions.assumingThat;
import org.junit.jupiter.api.Test;
class AssumptionsDemo {
@Test
void testOnlyOnCiServer() {
assumeTrue("CI".equals(System.getenv("ENV")));
// remainder of test
}
@Test
void testOnlyOnDeveloperWorkstation() {
assumeTrue("DEV".equals(System.getenv("ENV")),
() -> "Aborting test: not on developer workstation");
// remainder of test
}
@Test
void testInAllEnvironments() {
assumingThat("CI".equals(System.getenv("ENV")),
() -> {
// perform these assertions only on the CI server
assertEquals(2, 2);
});
14
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@Disabled
class DisabledClassDemo {
@Test
void testWillBeSkipped() {
}
}
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
class DisabledTestsDemo {
@Disabled
@Test
void testWillBeSkipped() {
}
@Test
void testWillBeExecuted() {
}
}
Composed Annotations
Note that any of the conditional annotations listed in the following sections may
also be used as a meta-annotation in order to create a custom composed
annotation. For example, the @TestOnMac annotation in the @EnabledOnOs demo
shows how you can combine @Test and @EnabledOnOs in a single, reusable
annotation.
15
Each of the conditional annotations listed in the following sections can only be
declared once on a given test interface, test class, or test method. If a conditional
annotation is directly present, indirectly present, or meta-present multiple times
on a given element, only the first such annotation discovered by JUnit will be used;
any additional declarations will be silently ignored. Note, however, that each
conditional annotation may be used in conjunction with other conditional
annotations in the org.junit.jupiter.api.condition package.
A container or test may be enabled or disabled on a particular operating system via the
@EnabledOnOs and @DisabledOnOs annotations.
@Test
@EnabledOnOs(MAC)
void onlyOnMacOs() {
// ...
}
@TestOnMac
void testOnMac() {
// ...
}
@Test
@EnabledOnOs({ LINUX, MAC })
void onLinuxOrMac() {
// ...
}
@Test
@DisabledOnOs(WINDOWS)
void notOnWindows() {
// ...
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Test
@EnabledOnOs(MAC)
@interface TestOnMac {
}
A container or test may be enabled or disabled on a particular version of the Java Runtime
Environment (JRE) via the @EnabledOnJre and @DisabledOnJre annotations.
16
@Test
@EnabledOnJre(JAVA_8)
void onlyOnJava8() {
// ...
}
@Test
@EnabledOnJre({ JAVA_9, JAVA_10 })
void onJava9Or10() {
// ...
}
@Test
@DisabledOnJre(JAVA_9)
void notOnJava9() {
// ...
}
A container or test may be enabled or disabled based on the value of the named JVM system property
via the @EnabledIfSystemProperty and @DisabledIfSystemProperty annotations. The value supplied
via the matches attribute will be interpreted as a regular expression.
@Test
@EnabledIfSystemProperty(named = "os.arch", matches = ".*64.*")
void onlyOn64BitArchitectures() {
// ...
}
@Test
@DisabledIfSystemProperty(named = "ci-server", matches = "true")
void notOnCiServer() {
// ...
}
A container or test may be enabled or disabled based on the value of the named environment
variable from the underlying operating system via the @EnabledIfEnvironmentVariable and
@DisabledIfEnvironmentVariable annotations. The value supplied via the matches attribute will be
interpreted as a regular expression.
17
@Test
@EnabledIfEnvironmentVariable(named = "ENV", matches = "staging-server")
void onlyOnStagingServer() {
// ...
}
@Test
@DisabledIfEnvironmentVariable(named = "ENV", matches = ".*development.*")
void notOnDeveloperWorkstation() {
// ...
}
JUnit Jupiter provides the ability to either enable or disable a container or test depending on the
evaluation of a script configured via the @EnabledIf or @DisabledIf annotation. Scripts can be
written in JavaScript, Groovy, or any other scripting language for which there is support for the
Java Scripting API, defined by JSR 223.
If the logic of your script depends only on the current operating system, the
current Java Runtime Environment version, a particular JVM system property, or a
particular environment variable, you should consider using one of the built-in
annotations dedicated to that purpose. See the previous sections of this chapter for
further details.
If you find yourself using the same script-based condition many times, consider
writing a dedicated ExecutionCondition extension in order to implement the
condition in a faster, type-safe, and more maintainable manner.
18
@Test // Static JavaScript expression.
@EnabledIf("2 * 3 == 6")
void willBeExecuted() {
// ...
}
@Test
@EnabledIf("'CI' == systemEnvironment.get('ENV')")
void onlyOnCiServer() {
assertTrue("CI".equals(System.getenv("ENV")));
}
Script Bindings
The following names are bound to each script context and therefore usable within the script. An
accessor provides access to a map-like structure via a simple String get(String name) method.
19
Name Type Description
systemProperty accessor JVM system property accessor.
junitConfiguration accessor Configuration parameter accessor.
Parameter
junitDisplayName String Display name of the test or container.
junitTags Set<String> All tags assigned to the test or container.
junitUniqueId String Unique ID of the test or container.
• A trimmed tag must not contain any of the following reserved characters.
◦ ,: comma
◦ (: left parenthesis
◦ ): right parenthesis
◦ &: ampersand
◦ |: vertical bar
◦ !: exclamation point
In the above context, "trimmed" means that leading and trailing whitespace
characters have been removed.
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@Tag("fast")
@Tag("model")
class TaggingDemo {
@Test
@Tag("taxes")
void testingTaxCalculation() {
}
20
3.9. Test Instance Lifecycle
In order to allow individual test methods to be executed in isolation and to avoid unexpected side
effects due to mutable test instance state, JUnit creates a new instance of each test class before
executing each test method (see note below for what qualifies as a test method). This "per-method"
test instance lifecycle is the default behavior in JUnit Jupiter and is analogous to all previous
versions of JUnit.
If you would prefer that JUnit Jupiter execute all test methods on the same test instance, simply
annotate your test class with @TestInstance(Lifecycle.PER_CLASS). When using this mode, a new test
instance will be created once per test class. Thus, if your test methods rely on state stored in
instance variables, you may need to reset that state in @BeforeEach or @AfterEach methods.
The "per-class" mode has some additional benefits over the default "per-method" mode. Specifically,
with the "per-class" mode it becomes possible to declare @BeforeAll and @AfterAll on non-static
methods as well as on interface default methods. The "per-class" mode therefore also makes it
possible to use @BeforeAll and @AfterAll methods in @Nested test classes.
If you are authoring tests using the Kotlin programming language, you may also find it easier to
implement @BeforeAll and @AfterAll methods by switching to the "per-class" test instance lifecycle
mode.
In the context of test instance lifecycle a test method is any method annotated with
@Test, @RepeatedTest, @ParameterizedTest, @TestFactory, or @TestTemplate.
If a test class or test interface is not annotated with @TestInstance, JUnit Jupiter will use a default
lifecycle mode. The standard default mode is PER_METHOD; however, it is possible to change the
default for the execution of an entire test plan. To change the default test instance lifecycle mode,
simply set the junit.jupiter.testinstance.lifecycle.default configuration parameter to the name
of an enum constant defined in TestInstance.Lifecycle, ignoring case. This can be supplied as a
JVM system property, as a configuration parameter in the LauncherDiscoveryRequest that is passed to
the Launcher, or via the JUnit Platform configuration file (see Configuration Parameters for details).
For example, to set the default test instance lifecycle mode to Lifecycle.PER_CLASS, you can start
your JVM with the following system property.
-Djunit.jupiter.testinstance.lifecycle.default=per_class
Note, however, that setting the default test instance lifecycle mode via the JUnit Platform
configuration file is a more robust solution since the configuration file can be checked into a
version control system along with your project and can therefore be used within IDEs and your
build software.
To set the default test instance lifecycle mode to Lifecycle.PER_CLASS via the JUnit Platform
configuration file, create a file named junit-platform.properties in the root of the class path (e.g.,
src/test/resources) with the following content.
junit.jupiter.testinstance.lifecycle.default = per_class
21
Changing the default test instance lifecycle mode can lead to unpredictable results
and fragile builds if not applied consistently. For example, if the build configures
"per-class" semantics as the default but tests in the IDE are executed using "per-
method" semantics, that can make it difficult to debug errors that occur on the
build server. It is therefore recommended to change the default in the JUnit
Platform configuration file instead of via a JVM system property.
import java.util.EmptyStackException;
import java.util.Stack;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@DisplayName("A stack")
class TestingAStackDemo {
Stack<Object> stack;
@Test
@DisplayName("is instantiated with new Stack()")
void isInstantiatedWithNew() {
new Stack<>();
}
@Nested
@DisplayName("when new")
class WhenNew {
@BeforeEach
void createNewStack() {
stack = new Stack<>();
}
@Test
@DisplayName("is empty")
void isEmpty() {
22
assertTrue(stack.isEmpty());
}
@Test
@DisplayName("throws EmptyStackException when popped")
void throwsExceptionWhenPopped() {
assertThrows(EmptyStackException.class, () -> stack.pop());
}
@Test
@DisplayName("throws EmptyStackException when peeked")
void throwsExceptionWhenPeeked() {
assertThrows(EmptyStackException.class, () -> stack.peek());
}
@Nested
@DisplayName("after pushing an element")
class AfterPushing {
@BeforeEach
void pushAnElement() {
stack.push(anElement);
}
@Test
@DisplayName("it is no longer empty")
void isNotEmpty() {
assertFalse(stack.isEmpty());
}
@Test
@DisplayName("returns the element when popped and is empty")
void returnElementWhenPopped() {
assertEquals(anElement, stack.pop());
assertTrue(stack.isEmpty());
}
@Test
@DisplayName("returns the element when peeked but remains not empty")
void returnElementWhenPeeked() {
assertEquals(anElement, stack.peek());
assertFalse(stack.isEmpty());
}
}
}
}
23
Only non-static nested classes (i.e. inner classes) can serve as @Nested test classes.
Nesting can be arbitrarily deep, and those inner classes are considered to be full
members of the test class family with one exception: @BeforeAll and @AfterAll
methods do not work by default. The reason is that Java does not allow static
members in inner classes. However, this restriction can be circumvented by
annotating a @Nested test class with @TestInstance(Lifecycle.PER_CLASS) (see Test
Instance Lifecycle).
ParameterResolver defines the API for test extensions that wish to dynamically resolve parameters at
runtime. If a test constructor or a @Test, @TestFactory, @BeforeEach, @AfterEach, @BeforeAll, or
@AfterAll method accepts a parameter, the parameter must be resolved at runtime by a registered
ParameterResolver.
There are currently three built-in resolvers that are registered automatically.
TestInfo acts as a drop-in replacement for the TestName rule from JUnit 4. The following
demonstrates how to have TestInfo injected into a test constructor, @BeforeEach method, and
@Test method.
24
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
@DisplayName("TestInfo Demo")
class TestInfoDemo {
TestInfoDemo(TestInfo testInfo) {
assertEquals("TestInfo Demo", testInfo.getDisplayName());
}
@BeforeEach
void init(TestInfo testInfo) {
String displayName = testInfo.getDisplayName();
assertTrue(displayName.equals("TEST 1") || displayName.equals("test2()"));
}
@Test
@DisplayName("TEST 1")
@Tag("my-tag")
void test1(TestInfo testInfo) {
assertEquals("TEST 1", testInfo.getDisplayName());
assertTrue(testInfo.getTags().contains("my-tag"));
}
@Test
void test2() {
}
In JUnit Jupiter you should use TestReporter where you used to print information to stdout or
25
stderr in JUnit 4. Using @RunWith(JUnitPlatform.class) will even output all reported entries to
stdout.
import java.util.HashMap;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;
class TestReporterDemo {
@Test
void reportSingleValue(TestReporter testReporter) {
testReporter.publishEntry("a key", "a value");
}
@Test
void reportSeveralValues(TestReporter testReporter) {
HashMap<String, String> values = new HashMap<>();
values.put("user name", "dk38");
values.put("award year", "1974");
testReporter.publishEntry(values);
}
Check out the MockitoExtension for an example of a custom ParameterResolver. While not intended
to be production-ready, it demonstrates the simplicity and expressiveness of both the extension
model and the parameter resolution process. MyMockitoTest demonstrates how to inject Mockito
mocks into @BeforeEach and @Test methods.
26
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import com.example.Person;
import com.example.mockito.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class MyMockitoTest {
@BeforeEach
void init(@Mock Person person) {
when(person.getName()).thenReturn("Dilbert");
}
@Test
void simpleTestWithInjectedMock(@Mock Person person) {
assertEquals("Dilbert", person.getName());
}
27
@TestInstance(Lifecycle.PER_CLASS)
interface TestLifecycleLogger {
@BeforeAll
default void beforeAllTests() {
LOG.info("Before all tests");
}
@AfterAll
default void afterAllTests() {
LOG.info("After all tests");
}
@BeforeEach
default void beforeEachTest(TestInfo testInfo) {
LOG.info(() -> String.format("About to execute [%s]",
testInfo.getDisplayName()));
}
@AfterEach
default void afterEachTest(TestInfo testInfo) {
LOG.info(() -> String.format("Finished executing [%s]",
testInfo.getDisplayName()));
}
interface TestInterfaceDynamicTestsDemo {
@TestFactory
default Collection<DynamicTest> dynamicTestsFromCollection() {
return Arrays.asList(
dynamicTest("1st dynamic test in test interface", () -> assertTrue(true)),
dynamicTest("2nd dynamic test in test interface", () -> assertEquals(4, 2
* 2))
);
}
@ExtendWith and @Tag can be declared on a test interface so that classes that implement the interface
automatically inherit its tags and extensions. See Before and After Test Execution Callbacks for the
source code of the TimingExtension.
28
@Tag("timed")
@ExtendWith(TimingExtension.class)
interface TimeExecutionLogger {
}
In your test class you can then implement these test interfaces to have them applied.
@Test
void isEqualValue() {
assertEquals(1, 1, "is always equal");
}
:junitPlatformTest
INFO example.TestLifecycleLogger - Before all tests
INFO example.TestLifecycleLogger - About to execute [dynamicTestsFromCollection()]
INFO example.TimingExtension - Method [dynamicTestsFromCollection] took 13 ms.
INFO example.TestLifecycleLogger - Finished executing [dynamicTestsFromCollection()]
INFO example.TestLifecycleLogger - About to execute [isEqualValue()]
INFO example.TimingExtension - Method [isEqualValue] took 1 ms.
INFO example.TestLifecycleLogger - Finished executing [isEqualValue()]
INFO example.TestLifecycleLogger - After all tests
BUILD SUCCESSFUL
Another possible application of this feature is to write tests for interface contracts. For example,
you can write tests for how implementations of Object.equals or Comparable.compareTo should
29
behave as follows.
T createValue();
T createNotEqualValue();
@Test
default void valueEqualsItself() {
T value = createValue();
assertEquals(value, value);
}
@Test
default void valueDoesNotEqualNull() {
T value = createValue();
assertFalse(value.equals(null));
}
@Test
default void valueDoesNotEqualDifferentValue() {
T value = createValue();
T differentValue = createNotEqualValue();
assertNotEquals(value, differentValue);
assertNotEquals(differentValue, value);
}
30
public interface ComparableContract<T extends Comparable<T>> extends Testable<T> {
T createSmallerValue();
@Test
default void returnsZeroWhenComparedToItself() {
T value = createValue();
assertEquals(0, value.compareTo(value));
}
@Test
default void returnsPositiveNumberComparedToSmallerValue() {
T value = createValue();
T smallerValue = createSmallerValue();
assertTrue(value.compareTo(smallerValue) > 0);
}
@Test
default void returnsNegativeNumberComparedToSmallerValue() {
T value = createValue();
T smallerValue = createSmallerValue();
assertTrue(smallerValue.compareTo(value) < 0);
}
In your test class you can then implement both contract interfaces thereby inheriting the
corresponding tests. Of course you’ll have to implement the abstract methods.
@Override
public String createValue() {
return "foo";
}
@Override
public String createSmallerValue() {
return "bar"; // 'b' < 'f' in "foo"
}
@Override
public String createNotEqualValue() {
return "baz";
}
31
The above tests are merely meant as examples and therefore not complete.
The following example demonstrates how to declare a test named repeatedTest() that will be
automatically repeated 10 times.
@RepeatedTest(10)
void repeatedTest() {
// ...
}
In addition to specifying the number of repetitions, a custom display name can be configured for
each repetition via the name attribute of the @RepeatedTest annotation. Furthermore, the display
name can be a pattern composed of a combination of static text and dynamic placeholders. The
following placeholders are currently supported.
The default display name for a given repetition is generated based on the following pattern:
"repetition {currentRepetition} of {totalRepetitions}". Thus, the display names for individual
repetitions of the previous repeatedTest() example would be: repetition 1 of 10, repetition 2 of
10, etc. If you would like the display name of the @RepeatedTest method included in the name of
each repetition, you can define your own custom pattern or use the predefined
RepeatedTest.LONG_DISPLAY_NAME pattern. The latter is equal to "{displayName} :: repetition
{currentRepetition} of {totalRepetitions}" which results in display names for individual
repetitions like repeatedTest() :: repetition 1 of 10, repeatedTest() :: repetition 2 of 10, etc.
In order to retrieve information about the current repetition and the total number of repetitions
programmatically, a developer can choose to have an instance of RepetitionInfo injected into a
@RepeatedTest, @BeforeEach, or @AfterEach method.
The RepeatedTestsDemo class at the end of this section demonstrates several examples of repeated
tests.
The repeatedTest() method is identical to example from the previous section; whereas,
repeatedTestWithRepetitionInfo() demonstrates how to have an instance of RepetitionInfo injected
into a test to access the total number of repetitions for the current repeated test.
32
The next two methods demonstrate how to include a custom @DisplayName for the @RepeatedTest
method in the display name of each repetition. customDisplayName() combines a custom display
name with a custom pattern and then uses TestInfo to verify the format of the generated display
name. Repeat! is the {displayName} which comes from the @DisplayName declaration, and 1/1 comes
from {currentRepetition}/{totalRepetitions}. In contrast, customDisplayNameWithLongPattern() uses
the aforementioned predefined RepeatedTest.LONG_DISPLAY_NAME pattern.
repeatedTestInGerman() demonstrates the ability to translate display names of repeated tests into
foreign languages — in this case German, resulting in names for individual repetitions such as:
Wiederholung 1 von 5, Wiederholung 2 von 5, etc.
Since the beforeEach() method is annotated with @BeforeEach it will get executed before each
repetition of each repeated test. By having the TestInfo and RepetitionInfo injected into the
method, we see that it’s possible to obtain information about the currently executing repeated test.
Executing RepeatedTestsDemo with the INFO log level enabled results in the following output.
import java.util.logging.Logger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.TestInfo;
33
class RepeatedTestsDemo {
@BeforeEach
void beforeEach(TestInfo testInfo, RepetitionInfo repetitionInfo) {
int currentRepetition = repetitionInfo.getCurrentRepetition();
int totalRepetitions = repetitionInfo.getTotalRepetitions();
String methodName = testInfo.getTestMethod().get().getName();
logger.info(String.format("About to execute repetition %d of %d for %s", //
currentRepetition, totalRepetitions, methodName));
}
@RepeatedTest(10)
void repeatedTest() {
// ...
}
@RepeatedTest(5)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
assertEquals(5, repetitionInfo.getTotalRepetitions());
}
When using the ConsoleLauncher or the junitPlatformTest Gradle plugin with the unicode theme
enabled, execution of RepeatedTestsDemo results in the following output to the console.
34
├─ RepeatedTestsDemo ✔
│ ├─ repeatedTest() ✔
│ │ ├─ repetition 1 of 10 ✔
│ │ ├─ repetition 2 of 10 ✔
│ │ ├─ repetition 3 of 10 ✔
│ │ ├─ repetition 4 of 10 ✔
│ │ ├─ repetition 5 of 10 ✔
│ │ ├─ repetition 6 of 10 ✔
│ │ ├─ repetition 7 of 10 ✔
│ │ ├─ repetition 8 of 10 ✔
│ │ ├─ repetition 9 of 10 ✔
│ │ └─ repetition 10 of 10 ✔
│ ├─ repeatedTestWithRepetitionInfo(RepetitionInfo) ✔
│ │ ├─ repetition 1 of 5 ✔
│ │ ├─ repetition 2 of 5 ✔
│ │ ├─ repetition 3 of 5 ✔
│ │ ├─ repetition 4 of 5 ✔
│ │ └─ repetition 5 of 5 ✔
│ ├─ Repeat! ✔
│ │ └─ Repeat! 1/1 ✔
│ ├─ Details... ✔
│ │ └─ Details... :: repetition 1 of 1 ✔
│ └─ repeatedTestInGerman() ✔
│ ├─ Wiederholung 1 von 5 ✔
│ ├─ Wiederholung 2 von 5 ✔
│ ├─ Wiederholung 3 von 5 ✔
│ ├─ Wiederholung 4 von 5 ✔
│ └─ Wiederholung 5 von 5 ✔
@ParameterizedTest
@ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
void palindromes(String candidate) {
assertTrue(isPalindrome(candidate));
}
This parameterized test uses the @ValueSource annotation to specify a String array as the source of
arguments. When executing the above method, each invocation will be reported separately. For
instance, the ConsoleLauncher will print output similar to the following.
35
palindromes(String) ✔
├─ [1] racecar ✔
├─ [2] radar ✔
└─ [3] able was I ere I saw elba ✔
In order to use parameterized tests you need to add a dependency on the junit-jupiter-params
artifact. Please refer to Dependency Metadata for details.
Out of the box, JUnit Jupiter provides quite a few source annotations. Each of the following
subsections provides a brief overview and an example for each of them. Please refer to the JavaDoc
in the org.junit.jupiter.params.provider package for additional information.
@ValueSource
@ValueSource is one of the simplest possible sources. It lets you specify a single array of literal values
and can only be used for providing a single parameter per parameterized test invocation.
• short
• byte
• int
• long
• float
• double
• char
• java.lang.String
• java.lang.Class
For example, the following @ParameterizedTest method will be invoked three times, with the values
1, 2, and 3 respectively.
@ParameterizedTest
@ValueSource(ints = { 1, 2, 3 })
void testWithValueSource(int argument) {
assertTrue(argument > 0 && argument < 4);
}
@EnumSource
@EnumSource provides a convenient way to use Enum constants. The annotation provides an optional
names parameter that lets you specify which constants shall be used. If omitted, all constants will be
36
used like in the following example.
@ParameterizedTest
@EnumSource(TimeUnit.class)
void testWithEnumSource(TimeUnit timeUnit) {
assertNotNull(timeUnit);
}
@ParameterizedTest
@EnumSource(value = TimeUnit.class, names = { "DAYS", "HOURS" })
void testWithEnumSourceInclude(TimeUnit timeUnit) {
assertTrue(EnumSet.of(TimeUnit.DAYS, TimeUnit.HOURS).contains(timeUnit));
}
The @EnumSource annotation also provides an optional mode parameter that enables fine-grained
control over which constants are passed to the test method. For example, you can exclude names
from the enum constant pool or specify regular expressions as in the following examples.
@ParameterizedTest
@EnumSource(value = TimeUnit.class, mode = EXCLUDE, names = { "DAYS", "HOURS" })
void testWithEnumSourceExclude(TimeUnit timeUnit) {
assertFalse(EnumSet.of(TimeUnit.DAYS, TimeUnit.HOURS).contains(timeUnit));
assertTrue(timeUnit.name().length() > 5);
}
@ParameterizedTest
@EnumSource(value = TimeUnit.class, mode = MATCH_ALL, names = "^(M|N).+SECONDS$")
void testWithEnumSourceRegex(TimeUnit timeUnit) {
String name = timeUnit.name();
assertTrue(name.startsWith("M") || name.startsWith("N"));
assertTrue(name.endsWith("SECONDS"));
}
@MethodSource
@MethodSource allows you to refer to one or more factory methods of the test class. Such methods
must return a Stream, Iterable, Iterator, or array of arguments. In addition, such methods must not
accept any arguments. By default such methods must be static unless the test class is annotated
with @TestInstance(Lifecycle.PER_CLASS).
If you only need a single parameter, you can return a Stream of instances of the parameter type as
demonstrated by the following example.
37
@ParameterizedTest
@MethodSource("stringProvider")
void testWithSimpleMethodSource(String argument) {
assertNotNull(argument);
}
If you do not explicitly provide a factory method name via @MethodSource, JUnit Jupiter will search
for a factory method that has the same name as the current @ParameterizedTest method by
convention. This is demonstrated in the following example.
@ParameterizedTest
@MethodSource
void testWithSimpleMethodSourceHavingNoValue(String argument) {
assertNotNull(argument);
}
Streams for primitive types (DoubleStream, IntStream, and LongStream) are also supported as
demonstrated by the following example.
@ParameterizedTest
@MethodSource("range")
void testWithRangeMethodSource(int argument) {
assertNotEquals(9, argument);
}
If a test method declares multiple parameters, you need to return a collection or stream of
Arguments instances as shown below. Note that Arguments.of(Object…) is a static factory method
defined in the Arguments interface.
38
@ParameterizedTest
@MethodSource("stringIntAndListProvider")
void testWithMultiArgMethodSource(String str, int num, List<String> list) {
assertEquals(3, str.length());
assertTrue(num >=1 && num <=2);
assertEquals(2, list.size());
}
@CsvSource
@CsvSource allows you to express argument lists as comma-separated values (i.e., String literals).
@ParameterizedTest
@CsvSource({ "foo, 1", "bar, 2", "'baz, qux', 3" })
void testWithCsvSource(String first, int second) {
assertNotNull(first);
assertNotEquals(0, second);
}
@CsvSource uses a single quote ' as its quote character. See the 'baz, qux' value in the example
above and in the table below. An empty, quoted value '' results in an empty String; whereas, an
entirely empty value is interpreted as a null reference. An ArgumentConversionException is raised if
the target type of a null reference is a primitive type.
@CsvFileSource
@CsvFileSource lets you use CSV files from the classpath. Each line from a CSV file results in one
invocation of the parameterized test.
39
@ParameterizedTest
@CsvFileSource(resources = "/two-column.csv", numLinesToSkip = 1)
void testWithCsvFileSource(String first, int second) {
assertNotNull(first);
assertNotEquals(0, second);
}
two-column.csv
Country, reference
Sweden, 1
Poland, 2
"United States of America", 3
In contrast to the syntax used in @CsvSource, @CsvFileSource uses a double quote "
as the quote character. See the "United States of America" value in the example
above. An empty, quoted value "" results in an empty String; whereas, an entirely
empty value is interpreted as a null reference. An ArgumentConversionException is
raised if the target type of a null reference is a primitive type.
@ArgumentsSource
@ParameterizedTest
@ArgumentsSource(MyArgumentsProvider.class)
void testWithArgumentsSource(String argument) {
assertNotNull(argument);
}
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of("foo", "bar").map(Arguments::of);
}
}
Implicit Conversion
To support use cases like @CsvSource, JUnit Jupiter provides a number of built-in implicit type
converters. The conversion process depends on the declared type of each method parameter.
For example, if a @ParameterizedTest declares a parameter of type TimeUnit and the actual type
supplied by the declared source is a String, the string will be automatically converted into the
40
corresponding TimeUnit enum constant.
@ParameterizedTest
@ValueSource(strings = "SECONDS")
void testWithImplicitArgumentConversion(TimeUnit argument) {
assertNotNull(argument.name());
}
String instances are currently implicitly converted to the following target types.
Target Example
Type
boolean/ "true" → true
Boolean
byte/Byt "1" → (byte) 1
e
char/Cha "o" → 'o'
racter
short/Sh "1" → (short) 1
ort
int/Inte "1" → 1
ger
long/Lon "1" → 1L
g
float/Fl "1.0" → 1.0f
oat
double/D "1.0" → 1.0d
ouble
Enum "SECONDS" → TimeUnit.SECONDS
subclass
java.io. "/path/to/file" → new File("/path/to/file")
File
java.mat "123.456e789" → new BigDecimal("123.456e789")
h.BigDec
imal
java.mat "1234567890123456789" → new BigInteger("1234567890123456789")
h.BigInt
eger
java.net "http://junit.org/" → URI.create("http://junit.org/")
.URI
java.net "http://junit.org/" → new URL("http://junit.org/")
.URL
java.nio "UTF-8" → Charset.forName("UTF-8")
.charset
.Charset
java.nio "/path/to/file" → Paths.get("/path/to/file")
.file.Pa
th
41
Target Example
Type
java.tim "1970-01-01T00:00:00Z" → Instant.ofEpochMilli(0)
e.Instan
t
java.tim "2017-03-14T12:34:56.789" → LocalDateTime.of(2017, 3, 14, 12, 34, 56, 789_000_000)
e.LocalD
ateTime
java.tim "2017-03-14" → LocalDate.of(2017, 3, 14)
e.LocalD
ate
java.tim "12:34:56.789" → LocalTime.of(12, 34, 56, 789_000_000)
e.LocalT
ime
java.tim "2017-03-14T12:34:56.789Z" → OffsetDateTime.of(2017, 3, 14, 12, 34, 56, 789_000_000,
e.Offset ZoneOffset.UTC)
DateTime
java.tim "12:34:56.789Z" → OffsetTime.of(12, 34, 56, 789_000_000, ZoneOffset.UTC)
e.Offset
Time
java.tim "2017-03" → YearMonth.of(2017, 3)
e.YearMo
nth
java.tim "2017" → Year.of(2017)
e.Year
java.tim "2017-03-14T12:34:56.789Z" → ZonedDateTime.of(2017, 3, 14, 12, 34, 56, 789_000_000,
e.ZonedD ZoneOffset.UTC)
ateTime
java.uti "JPY" → Currency.getInstance("JPY")
l.Curren
cy
java.uti "en" → new Locale("en")
l.Locale
java.uti "d043e930-7b3b-48e3-bdbe-5a3ccfb833db" → UUID.fromString("d043e930-7b3b-48e3-bdbe-
l.UUID 5a3ccfb833db")
In addition to implicit conversion from strings to the target types listed in the above table, JUnit
Jupiter also provides a fallback mechanism for automatic conversion from a String to a given target
type if the target type declares exactly one suitable factory method or a factory constructor as
defined below.
• factory method: a non-private, static method declared in the target type that accepts a single
String argument and returns an instance of the target type. The name of the method can be
arbitrary and need not follow any particular convention.
• factory constructor: a non-private constructor in the target type that accepts a single String
argument.
42
For example, in the following @ParameterizedTest method, the Book argument will be created by
invoking the Book.fromTitle(String) factory method and passing "42 Cats" as the title of the book.
@ParameterizedTest
@ValueSource(strings = "42 Cats")
void testWithImplicitFallbackArgumentConversion(Book book) {
assertEquals("42 Cats", book.getTitle());
}
Explicit Conversion
Instead of relying on implicit argument conversion you may explicitly specify an ArgumentConverter
to use for a certain parameter using the @ConvertWith annotation like in the following example.
@ParameterizedTest
@EnumSource(TimeUnit.class)
void testWithExplicitArgumentConversion(
@ConvertWith(ToStringArgumentConverter.class) String argument) {
assertNotNull(TimeUnit.valueOf(argument));
}
@Override
protected Object convert(Object source, Class<?> targetType) {
assertEquals(String.class, targetType, "Can only convert to String");
return String.valueOf(source);
}
}
43
Explicit argument converters are meant to be implemented by test and extension authors. Thus,
junit-jupiter-params only provides a single explicit argument converter that may also serve as a
reference implementation: JavaTimeArgumentConverter. It is used via the composed annotation
JavaTimeConversionPattern.
@ParameterizedTest
@ValueSource(strings = { "01.01.2017", "31.12.2017" })
void testWithExplicitJavaTimeConverter(
@JavaTimeConversionPattern("dd.MM.yyyy") LocalDate argument) {
assertEquals(2017, argument.getYear());
}
By default, the display name of a parameterized test invocation contains the invocation index and
the String representation of all arguments for that specific invocation. However, you can customize
invocation display names via the name attribute of the @ParameterizedTest annotation like in the
following example.
When executing the above method using the ConsoleLauncher you will see output similar to the
following.
Placeholder Description
{index} the current invocation index (1-based)
{arguments} the complete, comma-separated arguments list
{0}, {1}, … an individual argument
Each invocation of a parameterized test has the same lifecycle as a regular @Test method. For
example, @BeforeEach methods will be executed before each invocation. Similar to Dynamic Tests,
44
invocations will appear one by one in the test tree of an IDE. You may at will mix regular @Test
methods and @ParameterizedTest methods within the same test class.
You may use ParameterResolver extensions with @ParameterizedTest methods. However, method
parameters that are resolved by argument sources need to come first in the argument list. Since a
test class may contain regular tests as well as parameterized tests with different parameter lists,
values from argument sources are not resolved for lifecycle methods (e.g. @BeforeEach) and test
class constructors.
@BeforeEach
void beforeEach(TestInfo testInfo) {
// ...
}
@ParameterizedTest
@ValueSource(strings = "foo")
void testWithRegularParameterResolver(String argument, TestReporter testReporter) {
testReporter.publishEntry("argument", argument);
}
@AfterEach
void afterEach(TestInfo testInfo) {
// ...
}
In addition to these standard tests a completely new kind of test programming model has been
introduced in JUnit Jupiter. This new kind of test is a dynamic test which is generated at runtime by
a factory method that is annotated with @TestFactory.
In contrast to @Test methods, a @TestFactory method is not itself a test case but rather a factory for
45
test cases. Thus, a dynamic test is the product of a factory. Technically speaking, a @TestFactory
method must return a Stream, Collection, Iterable, or Iterator of DynamicNode instances. Instantiable
subclasses of DynamicNode are DynamicContainer and DynamicTest. DynamicContainer instances are
composed of a display name and a list of dynamic child nodes, enabling the creation of arbitrarily
nested hierarchies of dynamic nodes. DynamicTest instances will then be executed lazily, enabling
dynamic and even non-deterministic generation of test cases.
Any Stream returned by a @TestFactory will be properly closed by calling stream.close(), making it
safe to use a resource such as Files.lines().
As with @Test methods, @TestFactory methods must not be private or static and may optionally
declare parameters to be resolved by ParameterResolvers.
As of JUnit Jupiter 5.1.0, dynamic tests must always be created by factory methods; however, this
might be complemented by a registration facility in a later release.
The following DynamicTestsDemo class demonstrates several examples of test factories and dynamic
tests.
The first method returns an invalid return type. Since an invalid return type cannot be detected at
compile time, a JUnitException is thrown when it is detected at runtime.
The next five methods are very simple examples that demonstrate the generation of a Collection,
Iterable, Iterator, or Stream of DynamicTest instances. Most of these examples do not really exhibit
dynamic behavior but merely demonstrate the supported return types in principle. However,
dynamicTestsFromStream() and dynamicTestsFromIntStream() demonstrate how easy it is to generate
dynamic tests for a given set of strings or a range of input numbers.
46
all three to DynamicTest.stream(). Although the non-deterministic behavior of
generateRandomNumberOfTests() is of course in conflict with test repeatability and should thus be
used with care, it serves to demonstrate the expressiveness and power of dynamic tests.
The last method generates a nested hierarchy of dynamic tests utilizing DynamicContainer.
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.api.DynamicNode;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.ThrowingConsumer;
class DynamicTestsDemo {
@TestFactory
Collection<DynamicTest> dynamicTestsFromCollection() {
return Arrays.asList(
dynamicTest("1st dynamic test", () -> assertTrue(true)),
dynamicTest("2nd dynamic test", () -> assertEquals(4, 2 * 2))
);
}
@TestFactory
Iterable<DynamicTest> dynamicTestsFromIterable() {
return Arrays.asList(
dynamicTest("3rd dynamic test", () -> assertTrue(true)),
dynamicTest("4th dynamic test", () -> assertEquals(4, 2 * 2))
);
47
}
@TestFactory
Iterator<DynamicTest> dynamicTestsFromIterator() {
return Arrays.asList(
dynamicTest("5th dynamic test", () -> assertTrue(true)),
dynamicTest("6th dynamic test", () -> assertEquals(4, 2 * 2))
).iterator();
}
@TestFactory
Stream<DynamicTest> dynamicTestsFromStream() {
return Stream.of("A", "B", "C")
.map(str -> dynamicTest("test" + str, () -> { /* ... */ }));
}
@TestFactory
Stream<DynamicTest> dynamicTestsFromIntStream() {
// Generates tests for the first 10 even integers.
return IntStream.iterate(0, n -> n + 2).limit(10)
.mapToObj(n -> dynamicTest("test" + n, () -> assertTrue(n % 2 == 0)));
}
@TestFactory
Stream<DynamicTest> generateRandomNumberOfTests() {
@Override
public boolean hasNext() {
current = random.nextInt(100);
return current % 7 != 0;
}
@Override
public Integer next() {
return current;
}
};
48
// Returns a stream of dynamic tests.
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
}
@TestFactory
Stream<DynamicNode> dynamicTestsWithContainers() {
return Stream.of("A", "B", "C")
.map(input -> dynamicContainer("Container " + input, Stream.of(
dynamicTest("not null", () -> assertNotNull(input)),
dynamicContainer("properties", Stream.of(
dynamicTest("length > 0", () -> assertTrue(input.length() > 0)),
dynamicTest("not empty", () -> assertFalse(input.isEmpty()))
))
)));
}
4. Running Tests
4.1. IDE Support
4.1.1. IntelliJ IDEA
IntelliJ IDEA supports running tests on the JUnit Platform since version 2016.2. For details please
see the post on the IntelliJ IDEA blog. Note, however, that it is recommended to use IDEA 2017.3 or
newer since these newer versions of IDEA will download the following JARs automatically based on
the API version used in the project: junit-platform-launcher, junit-jupiter-engine, and junit-
vintage-engine.
IntelliJ IDEA releases prior to IDEA 2017.3 bundle specific versions of JUnit 5. Thus,
if you want to use a newer version of JUnit Jupiter, execution of tests within the
IDE might fail due to version conflicts. In such cases, please follow the instructions
below to use a newer version of JUnit 5 than the one bundled with IntelliJ IDEA.
In order to use a different JUnit 5 version (e.g., 5.1.0), you may need to include the corresponding
versions of the junit-platform-launcher, junit-jupiter-engine, and junit-vintage-engine JARs in the
classpath.
// Only needed to run tests in a version of IntelliJ IDEA that bundles older versions
testRuntime("org.junit.platform:junit-platform-launcher:1.1.0")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.1.0")
testRuntime("org.junit.vintage:junit-vintage-engine:5.1.0")
49
Additional Maven Dependencies
<!-- Only needed to run tests in a version of IntelliJ IDEA that bundles older
versions -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
4.1.2. Eclipse
Eclipse IDE offers support for the JUnit Platform since the Eclipse Oxygen.1a (4.7.1a) release.
For more information on using JUnit 5 in Eclipse consult the official Eclipse support for JUnit 5
section of the Eclipse Project Oxygen.1a (4.7.1a) - New and Noteworthy documentation.
At the time of this writing, there is no direct support for running tests on the JUnit Platform within
IDEs other than IntelliJ IDEA and Eclipse. However, the JUnit team provides two intermediate
solutions so that you can go ahead and try out JUnit 5 within your IDE today. You can use the
Console Launcher manually or execute tests with a JUnit 4 based Runner.
The JUnit team has developed a very basic Gradle plugin that allows you to run any kind of test that
is supported by a TestEngine (e.g., JUnit 3, JUnit 4, JUnit Jupiter, Specsy, etc.). See build.gradle in the
junit5-gradle-consumer project for an example of the plugin in action.
To use the JUnit Gradle plugin, you first need to make sure that you are running Gradle 2.5 or
higher. Once you’ve done that, you can configure build.gradle as follows.
50
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.1.0'
}
}
Once the JUnit Gradle plugin has been applied, you can configure it as follows.
junitPlatform {
platformVersion '1.1.0' // optional, defaults to plugin version
reportsDir file('build/test-results/junit-platform') // this is the default
// enableStandardTestTask true
// selectors (optional)
// filters (optional)
// logManager (optional)
}
By default, the JUnit Gradle plugin disables the standard Gradle test task, but this can be
overridden via the enableStandardTestTask flag.
Configuring Selectors
By default, the plugin will scan your project’s output directories for tests. However, you can specify
which tests to execute explicitly using the selectors extension element.
51
junitPlatform {
// ...
selectors {
uris 'file:///foo.txt', 'http://example.com/'
uri 'foo:resource' ①
files 'foo.txt', 'bar.csv'
file 'qux.json' ②
directories 'foo/bar', 'bar/qux'
directory 'qux/bar' ③
packages 'com.acme.foo', 'com.acme.bar'
aPackage 'com.example.app' ④
classes 'com.acme.Foo', 'com.acme.Bar'
aClass 'com.example.app.Application' ⑤
methods 'com.acme.Foo#a', 'com.acme.Foo#b'
method 'com.example.app.Application#run(java.lang.String[])' ⑥
resources '/bar.csv', '/foo/input.json'
resource '/com/acme/my.properties' ⑦
modules 'foo', 'bar'
module 'baz' ⑧
}
// ...
}
① URIs
② Local files
③ Local directories
④ Packages
⑦ Classpath resources
⑧ Module names
Configuring Filters
You can configure filters for the test plan by using the filters extension. By default, all engines and
tags are included in the test plan. Only the default includeClassNamePattern (^.*Tests?$) is applied.
You can override the default pattern as in the following example. When you specify multiple
patterns, they are combined using OR semantics.
52
junitPlatform {
// ...
filters {
engines {
include 'junit-jupiter'
// exclude 'junit-vintage'
}
tags {
include 'fast', 'smoke & feature-a'
// exclude 'slow', 'ci'
}
packages {
include 'com.sample.included1', 'com.sample.included2'
// exclude 'com.sample.excluded1', 'com.sample.excluded2'
}
includeClassNamePattern '.*Spec'
includeClassNamePatterns '.*Test', '.*Tests'
}
// ...
}
If you supply a Test Engine ID via engines {include …} or engines {exclude …}, the JUnit Gradle
plugin will only run tests for the desired test engines. Similarly, if you supply tags or tag expressions
via tags {include …} or tags {exclude …}, the JUnit Gradle plugin will only run tests that are
tagged accordingly (e.g., via the @Tag annotation for JUnit Jupiter based tests). The same applies to
package names that can be included or excluded using packages {include …} or packages {exclude
…}.
Configuration Parameters
You can set configuration parameters to influence test discovery and execution by using the
configurationParameter or configurationParameters DSL. The former can be used to set a single
configuration parameter, while the latter takes a map of configuration parameters to set multiple
key-value pairs at once. All keys and values must be Strings.
junitPlatform {
// ...
configurationParameter 'junit.jupiter.conditions.deactivate', '*'
configurationParameters([
'junit.jupiter.extensions.autodetection.enabled': 'true',
'junit.jupiter.testinstance.lifecycle.default': 'per_class'
])
// ...
}
In order to have the JUnit Gradle plugin run any tests at all, a TestEngine implementation must be
53
on the classpath.
To configure support for JUnit Jupiter based tests, configure a testCompile dependency on the JUnit
Jupiter API and a testRuntime dependency on the JUnit Jupiter TestEngine implementation similar to
the following.
dependencies {
testCompile("org.junit.jupiter:junit-jupiter-api:5.1.0")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.1.0")
}
The JUnit Gradle plugin can run JUnit 4 based tests as long as you configure a testCompile
dependency on JUnit 4 and a testRuntime dependency on the JUnit Vintage TestEngine
implementation similar to the following.
dependencies {
testCompile("junit:junit:4.12")
testRuntime("org.junit.vintage:junit-vintage-engine:5.1.0")
}
JUnit uses the Java Logging APIs in the java.util.logging package (a.k.a. JUL) to emit warnings and
debug information. Please refer to the official documentation of LogManager for configuration
options.
Alternatively, it’s possible to redirect log messages to other logging frameworks such as Log4j or
Logback. To use a logging framework that provides a custom implementation of LogManager,
configure the logManager extension property of the JUnit Gradle plugin. This will set the
java.util.logging.manager system property to the supplied fully qualified class name of the
LogManager implementation to use. The example below demonstrates how to configure Log4j 2.x (see
Log4j JDK Logging Adapter for details).
junitPlatform {
logManager 'org.apache.logging.log4j.jul.LogManager'
}
Other logging frameworks provide different means to redirect messages logged using
java.util.logging. For example, for Logback you can use the JUL to SLF4J Bridge by adding an
additional dependency to the runtime classpath.
Once the JUnit Gradle plugin has been applied and configured, you have a new junitPlatformTest
task at your disposal.
Invoking gradlew junitPlatformTest (or gradlew test) from the command line will execute all tests
54
within the project whose class names match the regular expression supplied via the
includeClassNamePattern (which defaults to ^.*Tests?$).
Executing the junitPlatformTest task in the junit5-gradle-consumer project results in output similar
to the following:
:junitPlatformTest
BUILD SUCCESSFUL
If a test fails, the build will fail with output similar to the following:
55
:junitPlatformTest
:junitPlatformTest FAILED
4.2.2. Maven
The JUnit team has developed a very basic provider for Maven Surefire that lets you run JUnit 4 and
JUnit Jupiter tests via mvn test. The pom.xml file in the junit5-maven-consumer project demonstrates
how to use it and can serve as a starting point.
Due to a memory leak in Surefire 2.20 and issues running on Java 9, the junit-
platform-surefire-provider currently only works with Surefire 2.19.1.
56
...
<build>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
...
In order to have Maven Surefire run any tests at all, a TestEngine implementation must be added to
the runtime classpath.
To configure support for JUnit Jupiter based tests, configure a test dependency on the JUnit Jupiter
API, and add the JUnit Jupiter TestEngine implementation to the dependencies of the maven-
surefire-plugin similar to the following.
57
...
<build>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
...
<dependencies>
...
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
...
The JUnit Platform Surefire Provider can run JUnit 4 based tests as long as you configure a test
dependency on JUnit 4 and add the JUnit Vintage TestEngine implementation to the dependencies of
the maven-surefire-plugin similar to the following.
58
...
<build>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
...
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
...
<dependencies>
...
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
...
The JUnit Platform Surefire Provider supports the test JVM system property supported by the
Maven Surefire Plugin. For example, to run only test methods in the org.example.MyTest test class
you can execute mvn -Dtest=org.example.MyTest test from the command line. For further details,
consult the Maven Surefire Plugin documentation.
The Maven Surefire Plugin will scan for test classes whose fully qualified names match the
following patterns.
• **/Test*.java
• **/*Test.java
59
• **/*TestCase.java
Note, however, that you can override this default behavior by configuring explicit include and
exclude rules in your pom.xml file. See the Inclusions and Exclusions of Tests documentation for
Maven Surefire for details.
Filtering by Tags
You can filter tests by tags or tag expressions using the following configuration properties.
...
<build>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<properties>
<includeTags>acceptance | !feature-a</includeTags>
<excludeTags>integration, regression</excludeTags>
</properties>
</configuration>
<dependencies>
...
</dependencies>
</plugin>
</plugins>
</build>
...
Configuration Parameters
You can set configuration parameters to influence test discovery and execution by using the
configurationParameters property and providing key-value pairs in the Java Properties file syntax.
60
...
<build>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<properties>
<configurationParameters>
junit.jupiter.conditions.deactivate = *
junit.jupiter.extensions.autodetection.enabled = true
junit.jupiter.testinstance.lifecycle.default = per_class
</configurationParameters>
</properties>
</configuration>
<dependencies>
...
</dependencies>
</plugin>
</plugins>
</build>
...
61
├─ JUnit Vintage
│ └─ example.JUnit4Tests
│ └─ standardJUnit4Test ✔
└─ JUnit Jupiter
├─ StandardTests
│ ├─ succeedingTest() ✔
│ └─ skippedTest() ↷ for demonstration purposes
└─ A special test case
├─ Custom test name containing spaces ✔
├─ ╯°□°)╯ ✔
└─ ὣ ✔
Exit Code
The ConsoleLauncher exits with a status code of 1 if any containers or tests failed.
Otherwise the exit code is 0.
4.3.1. Options
Option Description
------ -----------
-h, --help Display help information.
--disable-ansi-colors Disable ANSI colors in output (not
supported by all terminals).
--details <[none,summary,flat,tree,verbose] Select an output details mode for when
> tests are executed. Use one of:
[none,
summary, flat, tree, verbose]. If
'none'
is selected, then only the summary
and
test failures are shown. (default:
tree)
--details-theme <[ascii,unicode]> Select an output details tree theme for
when tests are executed. Use one of:
62
[ascii, unicode] (default: unicode)
--class-path, --classpath, --cp <Path: Provide additional classpath entries --
path1:path2:...> for example, for adding engines and
their dependencies. This option can
be
repeated.
--reports-dir <Path> Enable report output into a specified
local directory (will be created if
it
does not exist).
--scan-modules EXPERIMENTAL: Scan all resolved modules
for test discovery.
-o, --select-module <String: module name> EXPERIMENTAL: Select single module for
test discovery. This option can be
repeated.
--scan-class-path, --scan-classpath [Path: Scan all directories on the classpath
or
path1:path2:...] explicit classpath roots. Without
arguments, only directories on the
system classpath as well as
additional
classpath entries supplied via -cp
(directories and JAR files) are
scanned.
Explicit classpath roots that are not
on
the classpath will be silently
ignored.
This option can be repeated.
-u, --select-uri <URI> Select a URI for test discovery. This
option can be repeated.
-f, --select-file <String> Select a file for test discovery. This
option can be repeated.
-d, --select-directory <String> Select a directory for test discovery.
This option can be repeated.
-p, --select-package <String> Select a package for test discovery.
This
option can be repeated.
-c, --select-class <String> Select a class for test discovery. This
option can be repeated.
-m, --select-method <String> Select a method for test discovery.
This
option can be repeated.
-r, --select-resource <String> Select a classpath resource for test
discovery. This option can be
repeated.
-n, --include-classname <String> Provide a regular expression to include
only classes whose fully qualified
names
match. To avoid loading classes
unnecessarily, the default pattern
63
only
includes class names that end with
"Test" or "Tests". When this option
is
repeated, all patterns will be
combined
using OR semantics. (default:
^.*Tests?$)
-N, --exclude-classname <String> Provide a regular expression to exclude
those classes whose fully qualified
names match. When this option is
repeated, all patterns will be
combined
using OR semantics.
--include-package <String> Provide a package to be included in the
test run. This option can be
repeated.
--exclude-package <String> Provide a package to be excluded from
the
test run. This option can be
repeated.
-t, --include-tag <String> Provide a tag or tag expression to
include
only tests whose tags match. When
this
option is repeated, all patterns will
be
combined using OR semantics.
-T, --exclude-tag <String> Provide a tag or tag expression to
exclude
those tests whose tags match. When
this
option is repeated, all patterns will
be
combined using OR semantics.
-e, --include-engine <String> Provide the ID of an engine to be
included
in the test run. This option can be
repeated.
-E, --exclude-engine <String> Provide the ID of an engine to be
excluded
from the test run. This option can be
repeated.
--config <key=value> Set a configuration parameter for test
discovery and execution. This option
can
be repeated.
64
4.4. Using JUnit 4 to run the JUnit Platform
The JUnitPlatform runner is a JUnit 4 based Runner which enables you to run any test whose
programming model is supported on the JUnit Platform in a JUnit 4 environment — for example, a
JUnit Jupiter test class.
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.
Since the JUnit Platform has features that JUnit 4 does not have, the runner is only
able to support a subset of the JUnit Platform functionality, especially with regard
to reporting (see Display Names vs. Technical Names). But for the time being the
JUnitPlatform runner is an easy way to get started.
4.4.1. Setup
You need the following artifacts and their dependencies on the classpath. See Dependency Metadata
for details regarding group IDs, artifact IDs, and versions.
Explicit Dependencies
• junit-jupiter-api in test scope: API for writing tests using JUnit Jupiter, including @Test, etc.
• junit-jupiter-engine in test runtime scope: implementation of the TestEngine API for JUnit
Jupiter
Transitive Dependencies
To define a custom display name for the class run via @RunWith(JUnitPlatform.class) simply
annotate the class with @SuiteDisplayName and provide a custom value.
By default, display names will be used for test artifacts; however, when the JUnitPlatform runner is
used to execute tests with a build tool such as Gradle or Maven, the generated test report often
needs to include the technical names of test artifacts — for example, fully qualified class names —
instead of shorter display names like the simple name of a test class or a custom display name
containing special characters. To enable technical names for reporting purposes, simply declare the
@UseTechnicalNames annotation alongside @RunWith(JUnitPlatform.class).
65
Note that the presence of @UseTechnicalNames overrides any custom display name configured via
@SuiteDisplayName.
One way to use the JUnitPlatform runner is to annotate a test class with
@RunWith(JUnitPlatform.class) directly. Please note that the test methods in the following example
are annotated with org.junit.jupiter.api.Test (JUnit Jupiter), not org.junit.Test (JUnit Vintage).
Moreover, in this case the test class must be public; otherwise, some IDEs and build tools might not
recognize it as a JUnit 4 test class.
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
public class JUnit4ClassDemo {
@Test
void succeedingTest() {
/* no-op */
}
@Test
void failingTest() {
fail("Failing for failing's sake.");
}
If you have multiple test classes you can create a test suite as can be seen in the following example.
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.SuiteDisplayName;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit 4 Suite Demo")
@SelectPackages("example")
public class JUnit4SuiteDemo {
}
The JUnit4SuiteDemo will discover and run all tests in the example package and its subpackages. By
66
default, it will only include test classes whose names match the pattern ^.*Tests?$.
There are more configuration options for discovering and filtering tests than just
@SelectPackages. Please consult the Javadoc for further details.
• Deactivating Conditions
Configuration Parameters are text-based key-value pairs that can be supplied to test engines
running on the JUnit Platform via one of the following mechanisms.
3. The JUnit Platform configuration file: a file named junit-platform.properties in the root of the
class path that follows the syntax rules for a Java Properties file.
67
Operator Meaning Associativity
! not right
& and left
| or left
If you are tagging your tests across multiple dimensions, tag expressions help you to select which
tests to execute. Tagging by test type (e.g. micro, integration, end-to-end) and feature (e.g. foo, bar,
baz) the following tag expressions can be useful.
5. Extension Model
5.1. Overview
In contrast to the competing Runner, @Rule, and @ClassRule extension points in JUnit 4, the JUnit
Jupiter extension model consists of a single, coherent concept: the Extension API. Note, however,
that Extension itself is just a marker interface.
Developers can register one or more extensions declaratively by annotating a test interface, test
class, test method, or custom composed annotation with @ExtendWith(…) and supplying class
references for the extensions to register.
For example, to register a custom MockitoExtension for a particular test method, you would
annotate the test method as follows.
@ExtendWith(MockitoExtension.class)
@Test
void mockTest() {
// ...
}
68
To register a custom MockitoExtension for all tests in a particular class and its subclasses, you would
annotate the test class as follows.
@ExtendWith(MockitoExtension.class)
class MockTests {
// ...
}
@ExtendWith(FooExtension.class)
@ExtendWith(BarExtension.class)
class MyTestsV2 {
// ...
}
Developers can register extensions programmatically by annotating fields in test classes with
@RegisterExtension.
When an extension is registered declaratively via @ExtendWith, it can typically only be configured via
annotations. In contrast, when an extension is registered via @RegisterExtension, it can be
configured programmatically – for example, in order to pass arguments to the extension’s
constructor, a static factory method, or a builder API.
@RegisterExtension fields must not be private or null (at evaluation time) but may
be either static or non-static.
Static Fields
If a @RegisterExtension field is static, the extension will be registered after extensions that are
69
registered at the class level via @ExtendWith. Such static extensions are not limited in which
extension APIs they can implement. Extensions registered via static fields may therefore implement
class-level and instance-level extension APIs such as BeforeAllCallback, AfterAllCallback, and
TestInstancePostProcessor as well as method-level extension APIs such as BeforeEachCallback, etc.
In the following example, the server field in the test class is initialized programmatically by using a
builder pattern supported by the WebServerExtension. The configured WebServerExtension will be
automatically registered as an extension at the class level – for example, in order to start the server
before all tests in the class and then stop the server after all tests in the class have completed. In
addition, static lifecycle methods annotated with @BeforeAll or @AfterAll as well as @BeforeEach,
@AfterEach, and @Test methods can access the instance of the extension via the server field if
necessary.
class WebServerDemo {
@RegisterExtension
static WebServerExtension server = WebServerExtension.builder()
.enableSecurity(false)
.build();
@Test
void getProductList() {
WebClient webClient = new WebClient();
String serverUrl = server.getServerUrl();
// Use WebClient to connect to web server using serverUrl and verify response
assertEquals(200, webClient.get(serverUrl + "/products").getResponseStatus());
}
}
Instance Fields
If a @RegisterExtension field is non-static (i.e., an instance field), the extension will be registered
after the test class has been instantiated and after each registered TestInstancePostProcessor has
been given a chance to post-process the test instance (potentially injecting the instance of the
extension to be used into the annotated field). Thus, if such an instance extension implements class-
level or instance-level extension APIs such as BeforeAllCallback, AfterAllCallback, or
TestInstancePostProcessor, those APIs will not be honored. By default, an instance extension will be
registered after extensions that are registered at the method level via @ExtendWith; however, if the
test class is configured with @TestInstance(Lifecycle.PER_CLASS) semantics, an instance extension
will be registered before extensions that are registered at the method level via @ExtendWith.
In the following example, the docs field in the test class is initialized programmatically by invoking
a custom lookUpDocsDir() method and supplying the result to the static forPath() factory method in
the DocumentationExtension. The configured DocumentationExtension will be automatically registered
as an extension at the method level. In addition, @BeforeEach, @AfterEach, and @Test methods can
access the instance of the extension via the docs field if necessary.
70
An extension registered via an instance field
class DocumentationDemo {
@RegisterExtension
DocumentationExtension docs = DocumentationExtension.forPath(lookUpDocsDir());
@Test
void generateDocumentation() {
// use this.docs ...
}
}
Specifically, a custom extension can be registered by supplying its fully qualified class name in a file
named org.junit.jupiter.api.extension.Extension within the /META-INF/services folder in its
enclosing JAR file.
Auto-detection is an advanced feature and is therefore not enabled by default. To enable it, simply
set the junit.jupiter.extensions.autodetection.enabled configuration parameter to true. This can be
supplied as a JVM system property, as a configuration parameter in the LauncherDiscoveryRequest
that is passed to the Launcher, or via the JUnit Platform configuration file (see Configuration
Parameters for details).
For example, to enable auto-detection of extensions, you can start your JVM with the following
system property.
-Djunit.jupiter.extensions.autodetection.enabled=true
When auto-detection is enabled, extensions discovered via the ServiceLoader mechanism will be
added to the extension registry after JUnit Jupiter’s global extensions (e.g., support for TestInfo,
TestReporter, etc.).
Registered extensions are inherited within test class hierarchies with top-down semantics.
Similarly, extensions registered at the class-level are inherited at the method-level. Furthermore, a
specific extension implementation can only be registered once for a given extension context and its
71
parent contexts. Consequently, any attempt to register a duplicate extension implementation will be
ignored.
An ExecutionCondition is evaluated for each container (e.g., a test class) to determine if all the tests it
contains should be executed based on the supplied ExtensionContext. Similarly, an
ExecutionCondition is evaluated for each test to determine if a given test method should be executed
based on the supplied ExtensionContext.
When multiple ExecutionCondition extensions are registered, a container or test is disabled as soon
as one of the conditions returns disabled. Thus, there is no guarantee that a condition is evaluated
because another extension might have already caused a container or test to be disabled. In other
words, the evaluation works like the short-circuiting boolean OR operator.
See the source code of DisabledCondition and @Disabled for concrete examples.
Sometimes it can be useful to run a test suite without certain conditions being active. For example,
you may wish to run tests even if they are annotated with @Disabled in order to see if they are still
broken. To do this, simply provide a pattern for the junit.jupiter.conditions.deactivate
configuration parameter to specify which conditions should be deactivated (i.e., not evaluated) for
the current test run. The pattern can be supplied as a JVM system property, as a configuration
parameter in the LauncherDiscoveryRequest that is passed to the Launcher, or via the JUnit Platform
configuration file (see Configuration Parameters for details).
For example, to deactivate JUnit’s @Disabled condition, you can start your JVM with the following
system property.
-Djunit.jupiter.conditions.deactivate=org.junit.*DisabledCondition
Examples:
• org.junit.*: deactivates every condition under the org.junit base package and any of its
subpackages.
• *.MyCondition: deactivates every condition whose simple class name is exactly MyCondition.
• *System*: deactivates every condition whose simple class name contains System.
72
• org.example.MyCondition: deactivates the condition whose FQCN is exactly
org.example.MyCondition.
Common use cases include injecting dependencies into the test instance, invoking custom
initialization methods on the test instance, etc.
For concrete examples, consult the source code for the MockitoExtension and the SpringExtension.
• BeforeAllCallback
◦ BeforeEachCallback
▪ BeforeTestExecutionCallback
▪ AfterTestExecutionCallback
◦ AfterEachCallback
• AfterAllCallback
73
methods, implement BeforeEachCallback and AfterEachCallback instead.
The following example shows how to use these callbacks to calculate and log the execution time of
a test method. TimingExtension implements both BeforeTestExecutionCallback and
AfterTestExecutionCallback in order to time and log the test execution.
import java.lang.reflect.Method;
import java.util.logging.Logger;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
getStore(context).put(START_TIME, System.currentTimeMillis());
}
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
Method testMethod = context.getRequiredTestMethod();
long startTime = getStore(context).remove(START_TIME, long.class);
long duration = System.currentTimeMillis() - startTime;
Since the TimingExtensionTests class registers the TimingExtension via @ExtendWith, its tests will have
this timing applied when they execute.
74
A test class that uses the example TimingExtension
@ExtendWith(TimingExtension.class)
class TimingExtensionTests {
@Test
void sleep20ms() throws Exception {
Thread.sleep(20);
}
@Test
void sleep50ms() throws Exception {
Thread.sleep(50);
}
The following example shows an extension which will swallow all instances of IOException but
rethrow any other type of exception.
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable
throwable)
throws Throwable {
75
5.8. Providing Invocation Contexts for Test Templates
A @TestTemplate method can only be executed when at least one
TestTemplateInvocationContextProvider is registered. Each such provider is responsible for
providing a Stream of TestTemplateInvocationContext instances. Each context may specify a custom
display name and a list of additional extensions that will only be used for the next invocation of the
@TestTemplate method.
The following example shows how to write a test template as well as how to register and implement
a TestTemplateInvocationContextProvider.
76
A test template with accompanying extension
@TestTemplate
@ExtendWith(MyTestTemplateInvocationContextProvider.class)
void testTemplate(String parameter) {
assertEquals(3, parameter.length());
}
@Override
public Stream<TestTemplateInvocationContext>
provideTestTemplateInvocationContexts(ExtensionContext context) {
return Stream.of(invocationContext("foo"), invocationContext("bar"));
}
@Override
public List<Extension> getAdditionalExtensions() {
return Collections.singletonList(new ParameterResolver() {
@Override
public boolean supportsParameter(ParameterContext
parameterContext,
ExtensionContext extensionContext) {
return parameterContext.getParameter().getType().equals(
String.class);
}
@Override
public Object resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) {
return parameter;
}
});
}
};
}
}
77
In this example, the test template will be invoked twice. The display names of the invocations will
be “foo” and “bar” as specified by the invocation context. Each invocation registers a custom
ParameterResolver which is used to resolve the method parameter. The output when using the
ConsoleLauncher is as follows.
└─ testTemplate(String) ✔
├─ foo ✔
└─ bar ✔
ExtensionContext.Store.CloseableResource
An extension context store is bound to its extension context lifecycle. When an
extension context lifecycle ends it closes its associated store. All stored values that
are instances of CloseableResource are notified by an invocation of their close()
method.
AnnotationSupport provides static utility methods that operate on annotated elements (e.g.,
packages, annotations, classes, interfaces, constructors, methods, and fields). These include
methods to check whether an element is annotated or meta-annotated with a particular annotation,
to search for specific annotations, and to find annotated methods and fields in a class or interface.
Some of these methods search on implemented interfaces and within class hierarchies to find
78
annotations. Consult the JavaDoc for AnnotationSupport for further details.
ClassSupport provides static utility methods for working with classes (i.e., instances of
java.lang.Class). Consult the JavaDoc for ClassSupport for further details.
ReflectionSupport provides static utility methods that augment the standard JDK reflection and
class-loading mechanisms. These include methods to scan the classpath in search of classes
matching specified predicates, to load and create new instances of a class, and to find and invoke
methods. Some of these methods traverse class hierarchies to locate matching methods. Consult the
JavaDoc for ReflectionSupport for further details.
User-provided test and lifecycle methods are shown in orange, with callback code provided by
extensions shown in blue. The grey box denotes the execution of a single test method and will be
repeated for every test method in the test class.
The following table further explains the twelve steps in the User code and extension code diagram.
79
Ste Interface/An Description
p notation
1 interface extension code executed before all tests of the container are executed
org.junit.jup
iter.api.exte
nsion.BeforeA
llCallback
2 annotation user code executed before all tests of the container are executed
org.junit.jup
iter.api.Befo
reAll
3 interface extension code executed before each test is executed
org.junit.jup
iter.api.exte
nsion.BeforeE
achCallback
4 annotation user code executed before each test is executed
org.junit.jup
iter.api.Befo
reEach
5 interface extension code executed immediately before a test is executed
org.junit.jup
iter.api.exte
nsion.BeforeT
estExecutionC
allback
6 annotation user code of the actual test method
org.junit.jup
iter.api.Test
7 interface extension code for handling exceptions thrown during a test
org.junit.jup
iter.api.exte
nsion.TestExe
cutionExcepti
onHandler
8 interface extension code executed immediately after test execution and its
org.junit.jup corresponding exception handlers
iter.api.exte
nsion.AfterTe
stExecutionCa
llback
9 annotation user code executed after each test is executed
org.junit.jup
iter.api.Afte
rEach
10 interface extension code executed after each test is executed
org.junit.jup
iter.api.exte
nsion.AfterEa
chCallback
11 annotation user code executed after all tests of the container are executed
org.junit.jup
iter.api.Afte
rAll
80
Ste Interface/An Description
p notation
12 interface extension code executed after all tests of the container are executed
org.junit.jup
iter.api.exte
nsion.AfterAl
lCallback
In the simplest case only the actual test method will be executed (step 6); all other steps are optional
depending on the presence of user code or extension support for the corresponding lifecycle
callback. For further details on the various lifecycle callbacks please consult the respective JavaDoc
for each annotation and extension.
Instead, JUnit provides a gentle migration path via a JUnit Vintage test engine which allows existing
tests based on JUnit 3 and JUnit 4 to be executed using the JUnit Platform infrastructure. Since all
classes and annotations specific to JUnit Jupiter reside under a new org.junit.jupiter base
package, having both JUnit 4 and JUnit Jupiter in the classpath does not lead to any conflicts. It is
therefore safe to maintain existing JUnit 4 tests alongside JUnit Jupiter tests. Furthermore, since the
JUnit team will continue to provide maintenance and bug fix releases for the JUnit 4.x baseline,
developers have plenty of time to migrate to JUnit Jupiter on their own schedule.
See the example projects in the junit5-samples repository to find out how this is done with Gradle
and Maven.
For test classes or methods that are annotated with @Category, the JUnit Vintage test engine exposes
the category’s fully qualified class name as a tag of the corresponding test identifier. For example, if
a test method is annotated with @Category(Example.class), it will be tagged with "com.acme.Example".
Similar to the Categories runner in JUnit 4, this information can be used to filter the discovered
tests before executing them (see Running Tests for details).
81
• Annotations reside in the org.junit.jupiter.api package.
• @Before and @After no longer exist; use @BeforeEach and @AfterEach instead.
• @BeforeClass and @AfterClass no longer exist; use @BeforeAll and @AfterAll instead.
• @Rule and @ClassRule no longer exist; superseded by @ExtendWith; see the following section for
partial rule support.
The junit-jupiter-migrationsupport module from JUnit Jupiter currently supports the following
three Rule types including subclasses of those types:
• org.junit.rules.ExpectedException
As in JUnit 4, Rule-annotated fields as well as methods are supported. By using these class-level
extensions on a test class such Rule implementations in legacy code bases can be left unchanged
including the JUnit 4 rule import statements.
This limited form of Rule support can be switched on by the class-level annotation
org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport. This annotation is a
composed annotation which enables all migration support extensions: VerifierSupport,
ExternalResourceSupport, and ExpectedExceptionSupport.
However, if you intend to develop a new extension for JUnit 5 please use the new extension model
of JUnit Jupiter instead of the rule-based model of JUnit 4.
82
7. Advanced Topics
7.1. JUnit Platform Launcher API
One of the prominent goals of JUnit 5 is to make the interface between JUnit and its programmatic
clients – build tools and IDEs – more powerful and stable. The purpose is to decouple the internals
of discovering and executing tests from all the filtering and configuration that’s necessary from the
outside.
JUnit 5 introduces the concept of a Launcher that can be used to discover, filter, and execute tests.
Moreover, third party test libraries – like Spock, Cucumber, and FitNesse – can plug into the JUnit
Platform’s launching infrastructure by providing a custom TestEngine.
Introducing test discovery as a dedicated feature of the platform itself will (hopefully) free IDEs and
build tools from most of the difficulties they had to go through to identify test classes and test
methods in the past.
Usage Example:
import static
org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
83
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(
selectPackage("com.example.mytests"),
selectClass(MyTestClass.class)
)
.filters(
includeClassNamePatterns(".*Tests")
)
.build();
There’s currently the possibility to select classes, methods, and all classes in a package or even
search for all tests in the classpath. Discovery takes place across all participating test engines.
The resulting TestPlan is a hierarchical (and read-only) description of all engines, classes, and test
methods that fit the LauncherDiscoveryRequest. The client can traverse the tree, retrieve details
about a node, and get a link to the original source (like class, method, or file position). Every node in
the test plan has a unique ID that can be used to invoke a particular test or group of tests.
To execute tests, clients can use the same LauncherDiscoveryRequest as in the discovery phase or
create a new request. Test progress and reporting can be achieved by registering one or more
TestExecutionListener implementations with the Launcher as in the following example.
launcher.execute(request);
There is no return value for the execute() method, but you can easily use a listener to aggregate the
final results in an object of your own. For an example see the SummaryGeneratingListener.
84
7.1.3. Plugging in Your Own Test Engine
• junit-vintage-engine: A thin layer on top of JUnit 4 to allow running vintage tests with the
launcher infrastructure.
Third parties may also contribute their own TestEngine by implementing the interfaces in the junit-
platform-engine module and registering their engine. Engine registration is currently supported via
Java’s java.util.ServiceLoader mechanism. For example, the junit-jupiter-engine module registers
its org.junit.jupiter.engine.JupiterTestEngine in a file named
org.junit.platform.engine.TestEngine within the /META-INF/services in the junit-jupiter-engine
JAR.
In addition to the public Launcher API method for registering test execution listeners
programmatically, custom TestExecutionListener implementations discovered at runtime via Java’s
java.util.ServiceLoader facility are automatically registered with the DefaultLauncher. For example,
an example.TestInfoPrinter class implementing TestExecutionListener and declared within the
/META-INF/services/org.junit.platform.launcher.TestExecutionListener file is loaded and registered
automatically.
8. API Evolution
One of the major goals of JUnit 5 is to improve maintainers' capabilities to evolve JUnit despite its
being used in many projects. With JUnit 4 a lot of stuff that was originally added as an internal
construct only got used by external extension writers and tool builders. That made changing JUnit 4
especially difficult and sometimes impossible.
That’s why JUnit 5 introduces a defined lifecycle for all publicly available interfaces, classes, and
methods.
Status Description
INTERNAL Must not be used by any code other than JUnit itself. Might be removed
without prior notice.
DEPRECATED Should no longer be used; might disappear in the next minor release.
EXPERIMENTAL Intended for new, experimental features where we are looking for feedback.
Use this element with caution; it might be promoted to MAINTAINED or STABLE in
the future, but might also be removed without prior notice, even in a patch.
85
Status Description
MAINTAINED Intended for features that will not be changed in a backwards- incompatible
way for at least the next minor release of the current major version. If
scheduled for removal, it will be demoted to DEPRECATED first.
STABLE Intended for features that will not be changed in a backwards- incompatible
way in the current major version (5.*).
If the @API annotation is present on a type, it is considered to be applicable for all public members
of that type as well. A member is allowed to declare a different status value of lower stability.
86
Package Name Class Name Type
org.junit.jupiter.params.provi CsvSource annotation
der
org.junit.jupiter.params.provi EnumSource annotation
der
org.junit.jupiter.params.provi MethodSource annotation
der
org.junit.jupiter.params.provi ValueSource annotation
der
org.junit.jupiter.params.suppo AnnotationConsumer interface
rt
org.junit.platform.engine.disc ModuleSelector class
overy
9. Contributors
Browse the current list of contributors directly on GitHub.
87