JUnit 5
• Released in 2017
• Works only with Java version >= 8
• JUnit 5 = Platform + Jupiter + Vintage
• Supports JUnit 4 and so also JUnit 3
• Good to use with - maven-surefire-plugin >= 2.22.0
Components Overview
Third partyVintage Jupiter
Platform
Old tests New tests
Third party
tests
Build tools/IDEs
Foundation for discovering, filtering,
configuring and executing tests
• Launcher API – build tools & IDE
• TestEngine API
• ConsoleLauncher
TestEngine and APIs for:
• Writing Tests
• New annotations & methods
• Extensions
• Extension API
TestEngine for running JUnit 4 and
JUnit 3 based tests on the platform
Third party frameworks runs on
platform by making use of TestEngine
API
Assumptions
• Purpose
• Test only if certain resource is present
• Test only if environment variable has certain
value
• Profile based testing – Env., user etc.
• Contains subset of assumption methods from
JUnit 4
• New method assumingThat
• Throws AssumptionViolatedException to signal
that test has been aborted
Repeated
Tests
• Execute tests specified number of times
• Can get info about current repetition/iteration
and total repetition by passing RepetitionInfo as
method parameter
• Option to customize display name for each
repetition
Parameterized
Tests
• Need additional dependency
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
• Provide values through @ValueSource,
@EnumSource, @MethodSource, @CsvSource,
@CsvFileSource etc.
Test instance
Lifecycle
• Test class is instantiated at per test method
level by default (PER_METHOD)
• Same as Junit 4
• Even for @Disabled method test class is
instantiated
• Ways to change it:
• Class level instantiation -
@TestInstance(Lifecycle.PER_CLASS)
• JVM parameter –
-Djunit.jupiter.testinstance.lifecycle.default=per_class
• Prop. File
src/test/resources >
junit-platform.properties >
junit.jupiter.testinstance.lifecycle.default = per_class