JUnit 5 Tutorial | The Next Generation of JUnit

Junit 5 Tutorial The Next Generation of JUnit

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

1. Junit 5 Maven example

Demonstrates the bare minimum configuration for getting started with JUnit Jupiter using the Maven build system.

2. Junit 5 with gradle configuration with an example

Demonstrates the bare minimum configuration for getting started with JUnit Jupiter using the Gradle build system.

3. Junit 5 Dispaly names for tests

Demonstrates the display custom names for the tests, built-in creating custom Display name generator.

4. Junit 5 test instance lifecycle with exampless

Demonstrates the instance lifecycle of junit 5 test cases and the modes of test instance lifecycle.

5. Junit 5 Assumptions class examples

Demonstrates the Junit 5 assumptions class assumeTrue()/assumeFalse() and assumeThat() methods.

6. Junit 5 Disable tests @Disabled examples

Demonstrates the how to disable tests using @Disabled annotation.

7. Junit 5 conditional test execution with examples

Demonstrates various conditional tests supported by Junit 5 and creating custom condition.

8. Junit 5 repeated tests with examples

Demonstrates repeating tests execution by the specified number of times and creating a composed annotation to dispaly names with repetition info.

9. Junit 5 timeouts – Fail if test not executed within time

Demonstrates Junit 5 timeout tests using @Timeout, assertTimeout(), assertTimeoutPreemptively() and configuring global timeouts

10. Junit 5 test execution order with examples

Demonstrates the Junit 5 tests execution order, @MethodOrder, @Order annotations AlphanUmeric order and creating custom order.

11. Junit 5 Parameterized Tests with examples

Demonstrates the various argument sources to run parameterized tests, ArgumentsAccessor, Argument Aggregation examples.

12. Junit 5 ConsoleLauncher examples

Demonstrates the ConsoleLauncher to run Junit jupitor tests with several options.

13. Junit 5 with allure reports example

Demonstrates the Junit 5, allure, maven configuration and generating allure reports.

14. Junit 5 dynamic tests @TestFactory with examples

Demonstrates the creating dynamic tests in Junit 5 and ordering and parallel execution of dynamic tests.

15. Junit 5 nested tests with several examples

Demonstrates the creating nested tests, nested tests execution order and timeouts in nested tests.

16. Built-in and custom Junit 5 ParameterResolver examples

Dependency injection for test methods and constructors, built-in parameter resolvers and custom parameter resolvers to inject parameters.

17. Junit 5 @Tag and Filtering Tags with Examples

Demonstrates the tagging tests classes and methods filter or discovering tagged test to execute them.

18. Run tag-specific junit 5 tests from gradle command

Demonstrates the executing tag-specific test cases from gradle command line.

19. Junit 5 parallel tests execution and @ResourceLock examples

Demonstrates the configurations to run test classes and test methods parallel and @ResourceLock annotation to get synchronization access for the shared resources.

20. Spring Boot Junit 5 test example

Demonstrates the bare minimum configuration for getting started with JUnit Jupiter using the Maven build system and spring boot, simple tests for Rest controller.

21. JUnitPlatform runner – create test suites and run Junit 4 + Junit 5 tests

Demonstrates how to create test suites for JUnit 5 and JUnit 4 Tests in JUnit 4 Environment using JUnitPlatform runner

LEAVE A REPLY

Please enter your comment!
Please enter your name here