JUnit 5 is a next generation of Junit for Java 8 and greater versions. In this Junit 5 tutorial we have covered most of the examples for writing tests.
Unlike previous versions of JUnit, and JUnit 5 is composed of several different modules from three different sub-projects. In this Junit 5 tutorial we have focused on Junit Jupiter for writing tests and Junit platform for executing tests.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
Junit Platform : It is a Launcher for test frameworks on JVM, defines TestEngine API for discovering and executing tests, provides ConsoleLauncher for running Junit vintage and Junit Jupiter tests, and first class support for IDEs and build tools like Gradle, Maven etc.
Junit Jupiter : A new programming model for writing unit tests, an extension model for Junit 4 and provides standard assertions.
Junit Vintage : A TestEngine to run JUnit 3 & JUnit 4 tests and migration support to JUnit 5 (JUnit Jupiter).
In this JUnit 5 tutorial we have covered following concepts with examples for writing and executing tests.
Junit 5 Tutorial Examples
Demonstrates the bare minimum configuration for getting started with JUnit Jupiter using the Maven build system.
Demonstrates the bare minimum configuration for getting started with JUnit Jupiter using the Gradle build system.
Demonstrates the display custom names for the tests, built-in creating custom Display name generator.
Demonstrates the instance lifecycle of junit 5 test cases and the modes of test instance lifecycle.
Demonstrates the Junit 5 assumptions class assumeTrue()/assumeFalse() and assumeThat() methods.
Demonstrates the how to disable tests using @Disabled annotation.
Demonstrates various conditional tests supported by Junit 5 and creating custom condition.
Demonstrates repeating tests execution by the specified number of times and creating a composed annotation to dispaly names with repetition info.
Demonstrates Junit 5 timeout tests using @Timeout, assertTimeout(), assertTimeoutPreemptively() and configuring global timeouts
Demonstrates the Junit 5 tests execution order, @MethodOrder, @Order annotations AlphanUmeric order and creating custom order.
Demonstrates the various argument sources to run parameterized tests, ArgumentsAccessor, Argument Aggregation examples.
Demonstrates the ConsoleLauncher to run Junit jupitor tests with several options.
Demonstrates the Junit 5, allure, maven configuration and generating allure reports.
Demonstrates the creating dynamic tests in Junit 5 and ordering and parallel execution of dynamic tests.
Demonstrates the creating nested tests, nested tests execution order and timeouts in nested tests.
Dependency injection for test methods and constructors, built-in parameter resolvers and custom parameter resolvers to inject parameters.
Demonstrates the tagging tests classes and methods filter or discovering tagged test to execute them.
Demonstrates the executing tag-specific test cases from gradle command line.
Demonstrates the configurations to run test classes and test methods parallel and @ResourceLock annotation to get synchronization access for the shared resources.
Demonstrates the bare minimum configuration for getting started with JUnit Jupiter using the Maven build system and spring boot, simple tests for Rest controller.
Demonstrates how to create test suites for JUnit 5 and JUnit 4 Tests in JUnit 4 Environment using JUnitPlatform runner