In this tutorial we will learn how to use @BeforeEach, @AfterEach @BeforeAll and @AfterAll annotations to execute fixtures in your JUnit 5 Test Classes.

@BeforeEach

@BeforeEach is used to signal that the annotated method should be executed before each @Test method in the current test class. It is equivalent to JUnit 4’s @Before.

Example:

package com.example;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

class JUnit5ExampleTest {

    @BeforeEach
    void init(TestInfo testInfo) {
        String callingTest = testInfo.getTestMethod().get().getName();
        System.out.println(callingTest);
    }

    @Test
    void firstTest() {
        System.out.println("Executed firstTest");
    }

    @Test
    void secondTest() {
        System.out.println("Executed secondTest");
    }
}

Output:

firstTest
Executed firstTest
secondTest
Executed secondTest

@AfterEach

This annotation denotes that the annotated JUnit 5 method should be executed after each test method, analogous to JUnit 4’s @After. For example, if the tests need to reset a property after each test, we can annotate a method with @AfterEach for that task.

import org.junit.jupiter.api.*;

class JUnit5TestExample {

    @Test
    void firstTest() {
        System.out.println("Called firstTest");
    }

    @Test
    void secondTest() {
        System.out.println("Called secondTest2");
    }

    @AfterEach
    void after(TestInfo testInfo) {
        String callingTest = testInfo.getTestMethod().get().getName();
        System.out.println(callingTest);
    }
}

Output:

Called firstTest
firstTest
Called secondTest2
secondTest

@BeforeAll

This annotation executes a JUnit 5 method before all tests. This is analogous to JUnit 4’s @BeforeClass. The @BeforeAll annotation is typically used to initialize various things for the tests.

Example:

import org.junit.jupiter.api.*;

class JUnit5TestExample {

    @BeforeAll
    static void init() {
        System.out.println("Run only once before all tests");
    }

    @Test
    void firstTest() {
        System.out.println("Called firstTest");
    }

    @Test
    void secondTest() {
        System.out.println("Called secondTest2");
    }
}

Output:

Run only once before all tests
Called firstTest
Called secondTest2

@AfterAll

The @AfterAll annotation is used to execute the JUnit 5 annotated method, only after all tests have been executed. This is analogous to JUnit 4’s @AfterClass. We use this annotation to tear down or terminate all processes at the end of all tests.

Example:

import org.junit.jupiter.api.*;

class JUnit5TestExample {

    @Test
    void firstTest() {
        System.out.println("Called firstTest");
    }

    @Test
    void secondTest() {
        System.out.println("Called secondTest2");
    }

    @AfterAll
    static void after() {
        System.out.println("Run only once after all tests");
    }
}

Output:

Called firstTest
Called secondTest2
Run only once after all tests

junit lifecycle methods

Dependencies used to build the JUnit 5 example:

Maven:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.junit5</groupId>
    <artifactId>running-unit-tests</artifactId>
    <version>0.1</version>
    <name>Running Unit Tests</name>
    <description>
        This example demonstrates how you can run unit tests which use JUnit 5.
    </description>

    <properties>
        <jdk.version>1.8</jdk.version>
        <junit.jupiter.version>5.5.2</junit.jupiter.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>running-unit-tests</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
        </plugins>
    </build>
</project>

Gradle:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.2')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.5.2')
}

test {
    useJUnitPlatform()
    testLogging {
        showStandardStreams = true
    }
}
FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials