JUnit 5 Architecture

Hi all, in this article, we will learn the JUnit 5 architecture and its building blocks. JUnit is one of the most common unit testing and test runner framework. Some teams still using JUnit 4 but day by day JUnit 4 usage is decreasing. Thus, it is better to learn and use JUnit 5 and its new features.

JUnit 5 Dependency

Let’s start with dependencies. You can add JUnit 5 (a.k.a Jupiter) support to your project with below dependency. In order to use JUnit 5, you need to use JAVA JDK version 8.

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>

JUnit 5 Architecture

Junit 5 comprises several building blocks. The architecture of JUnit 5 is illustrated below.

Let’s discover some core elements of this architecture.

JUnit Platform

Its main responsibility is to launch the testing frameworks on the JVM. As you see in the architecture diagram it is an interface between build tools, tests, IDE and JUnit. It also defines TestEngine API to create a testing framework which operates on JUnit platform and in this way we can use external libraries in JUnit ecosystem by implementing custom engines.

JUnit Jupiter

This is the new extension and building block of JUnit. It comprises new extensions and libraries for JUnit 5. The new annotations of the JUnit 5 are summarized as follows:

  • @BeforeAll: Annotated method runs before all test methods in the current class.
  • @AfterAll: Annotated method runs after all test methods in the current class.
  • @BeforeEach: Annotated method runs before each test method.
  • @AfterEach: Annotated method runs after each test method.
  • @Disable: Disables a test class or a method.
  • @Tag: We can tag the tests methods such as Smoke, Regression, Critical, etc.
  • @Nested: Annotated class is a nested, non-static test class.
  • @DisplayName: We can declare a custom display name for a test class or a test method.
  • @ExtendWith: it is used to register custom extensions
  • @TestFactory: Declares that the method that is a test factory method for dynamic testıng.

JUnit Vintage

It is a supporting library for JUnit 5. By using Junit Vintage we can also run JUnit 3 and JUnit 4 based tests on the JUnit 5 platform.

JUnit 5 Maven Repositories

You can reach all JUnit maven libraries here.

JUnit Jupiter: https://mvnrepository.com/artifact/org.junit.jupiter

JUnit Platform: https://mvnrepository.com/artifact/org.junit.platform

JUnit Vintage: https://mvnrepository.com/artifact/org.junit.vintage

This is a brief overview of JUnit 5 architecture and its building blocks. We will cover the JUnit 5 annotations, Assertions, and the other features in other articles.

*Image Reference: http://dolszewski.com/resources/post/2017-05-17/junit5-architecture.png

Thanks.
Onur Baskirt

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.