Home Junit 5 JUnit 5 Tutorial

JUnit 5 Tutorial

Junit 5 tutorial

JUnit 5 is a simple opensource next generation of JUnit for Java 8 and greater versions. In our JUnit 5 tutorials we have covered most of the examples for writing tests. JUnit 5 requires Java 8 or higher at runtime.

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 tutorials we have covered following concepts with examples for writing and executing tests.

Junit 5 Tutorial Examples

1. Junit 5 + Maven

Tutorial to demonstrate the bare minimum configuration for getting started with JUnit Jupiter using the Maven build system.

2. Junit 5 + gradle

Tutorial to demonstrate the bare minimum configuration for getting started with JUnit Jupiter using the Gradle build system.

3. Junit 5 Dispaly names

Tutorial to demonstrate the display custom names for the tests, built-in and creating custom Display name generator.

4. Junit 5 test instance lifecycle

Tutorial to demonstrate the instance lifecycle of junit 5 test cases and the modes of test instance lifecycle.

5. Junit 5 Assumptions

Tutorial to demonstrate the Junit 5 Assumptions class assumeTrue()/assumeFalse() and assumeThat() methods.

6. Junit 5 Disable tests

Tutorial to demonstrate the how to disable tests using @Disabled annotation.

7. Junit 5 conditional tests

Demonstrate various conditional tests supported by JUnit 5 and how tcreating custom condition.

8. Junit 5 repeated tests

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

9. Junit 5 timeouts

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

10. Junit 5 test execution order

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

11. Junit 5 Parameterized Tests

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

12. Junit 5 ConsoleLauncher

Demonstrates the ConsoleLauncher to run JUnit Jupitertests with several options.

13. Junit 5 with allure reports

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

14. Junit 5 dynamic tests @TestFactory

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

15. Junit 5 nested tests

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

16. Built-in and custom Junit 5 ParameterResolver

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

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

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

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.

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles