JUnit 5
Last updated
© Leapwise
Last updated
JUnit 5 is a Java testing framework that provides a platform for writing and running tests. It introduces modern features, extensibility, and improved architecture over its predecessors, facilitating robust and flexible test development in Java applications.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
Junit Platform → It provides a core foundation to help launching testing frameworks on JVM. It acts as an interface between JUnit and its clients such as build tools (Maven and Gradle) and IDE's (Eclipse and IntelliJ). It introduces the concept of a “Launcher” which external tools use to discover, filter, and execute tests.
Junit Jupiter → It provides a new programming model and extension model for writing tests and extensions in Junit 5. It has a whole new annotation to write test cases in Junit 5. It implements TestEngine API provided by Junit Platform so that Junit 5 test can be run.
Junit Vintage → This sub-project provides extensive support for writing test cases in JUnit 4 and JUnit 3. Thus, backward compatibility is been provided by this project.
junit-jupiter-api
junit-platform-launcher
junit-vintage-engine
JUnit5 supports default (package), public and protected visibility, even if it is recommended to use the default (package) visibility, which improves the readability of code.
Test methods and lifecycle methods may be declared locally within the current test class, inherited from superclasses, or inherited from interfaces
Test methods and lifecycle methods must not be abstract
Ignores without any warning:
private classes and private methods
static methods
methods returning a value without being a TestFactory
Minimal requirements:
If not specified at the class level, the default behavior is the "per-method" test instance lifecycle. @TestInstance
, a type-level annotation, configures the test instance lifecycle for the annotated class or interface. Setting the mode to PER_CLASS
allows shared test instance state between methods, non-static @BeforeAll
, and @AfterAll
methods. @TestInstance
can be used as a meta-annotation to create custom composed annotations inheriting its semantics.