JUnit is one of the most popular unit-testing frameworks in the Java ecosystem. The JUnit 5 version contains a number of exciting innovations, with the goal to support new features in Java 8 and above, as well as enabling many different styles of testing.
Unlike previous versions of JUnit, JUnit 5 is composed of several different modules from three different sub-projects.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
The JUnit Platform serves as a foundation for launching testing frameworks on the JVM. It also defines the
TestEngineAPI 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 a JUnit 4 based Runner for running any
TestEngine on the platform in a JUnit 4 based environment. First-class support for the JUnit Platform also exists in popular IDEs (see IntelliJ IDEA, Eclipse) and build tools (see Gradle, Maven).
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.
Below you have just a taste of some of the new features JUnit 5 has got to offer. Check out the user guide for a more complete list.
A display name can now be set for a test. This will be shown instead of the method name and will make output both easier to read and format. You can even include spaces and emojis.
Dynamic on-the-fly tests can be made using @TestFactory and lambdas. These tests must return instances of Streams, Collections, Iterables, or Iterators of DynamicNode instances. These cases are executed lazily and so is generated at run-time.
Multiple assertions at once
We now have the power to group assertions together using the assertAll method. It will process all of the assertions in a group even if one fails. Then it will let us know which assertions failed at the end so we do not have to fix and rerun one at a time.
These were already present in JUnit 4 but I thought they were worth a mention because in JUnit 5 they can also use Java 8 lambdas. According to the JUnit 5 user guide “Assumptions provide a basic form of dynamic behavior but are intentionally rather limited in their expressiveness”. They are useful because a failed Assumption does not fail a test, it aborts it. This is useful if you only want to perform tests under certain conditions like on certain platforms or only if certain variables are present in the current runtime environment.
These allow you to run a test case multiple times with different arguments. These arguments can be strings, literal values, methods, Enums, CSV files, etc. @ParameterisedTest ultimately lets you avoid using unnecessary testing loops or duplicating test code.
@Timeout isn’t limited to being placed on test cases themselves. Along with the aforementioned test cases,
@Timeout can also be placed at the type level, where it will provide a default timeout for all test cases declared in the class, this can be overridden by adding a
@Timeout to a test case.
@Timeout can also be added to lifecycle methods,
@Testannotation no longer takes an expected argument. The Jupiter API provides a much tidier way of writing this type of test.
Temporary Directory Extension
TempDirectory extension is used to create and clean up a temporary directory for an individual test or all tests in a test class. It is registered by default. To use it, annotate a non-private field of type
@TempDir or add a parameter of type
java.io.File annotated with
@TempDir to a lifecycle method or test method.
Declarative Extension Registration
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.
AfterTestExecutionCallback define the APIs for
Extensions that wish to add behavior that will be executed immediately before and immediately after a test method is executed, respectively. As such, these callbacks are well suited for timing, tracing, and similar use cases. If you need to implement callbacks that are invoked around
@AfterEach methods, implement
Parallel Test execution
By default, JUnit Jupiter tests are run sequentially in a single thread. Running tests in parallel — for example, to speed up execution — is available as an opt-in feature since version 5.3. To enable parallel execution, set the
junit.jupiter.execution.parallel.enabled configuration parameter to
true — for example, in
junit-platform.properties (see Configuration Parameters for other options). Please note that enabling this property is only the first step required to execute tests in parallel. If enabled, test classes and methods will still be executed sequentially by default. Whether or not a node in the test tree is executed concurrently is controlled by its execution mode. The following two modes are available.
- SAME_THREAD: Force execution in the same thread used by the parent. For example, when used on a test method, the test method will be executed in the same thread as any @BeforeAll or @AfterAll methods of the containing test class.
- CONCURRENT: Execute concurrently unless a resource lock forces execution in the same thread.
Migration from JUnit 4
Although the JUnit Jupiter programming model and extension model will not support JUnit 4 features such as
Runners natively, it is not expected that source code maintainers will need to update all of their existing tests, test extensions, and custom build test infrastructure to migrate to JUnit Jupiter.
Running JUnit 4 Tests on the JUnit Platform
Just make sure that the
junit-vintage-engine artifact is in your test runtime path. In that case JUnit 4 tests will automatically be picked up by the JUnit Platform launcher.
The following are topics that you should be aware of when migrating existing JUnit 4 tests to JUnit Jupiter.
Annotations reside in the
Assertions reside in
Assumptions reside in
Note that JUnit Jupiter 5.4 and later versions support methods from JUnit 4’s
org.junit.Assumeclass for assumptions. Specifically, JUnit Jupiter supports JUnit 4’s
AssumptionViolatedExceptionto signal that a test should be aborted instead of marked as a failure.
@Afterno longer exist; use
@AfterClassno longer exist; use
@Ignoreno longer exists: use
@Disabledor one of the other built-in execution conditions instead
See also JUnit 4 @Ignore Support.
@Categoryno longer exists; use
@RunWithno longer exists; superseded by
@ClassRuleno longer exist; superseded by
See also Limited JUnit 4 Rule Support.