JUnit 5 @AfterEach

@AfterEach annotation is used to signal that the annotated method should be executed after each @Test @RepeatedTest@ParameterizedTest, or @TestFactory methods in the current class.

JUnit 5 @AfterEach annotation is a replacement of @After annotation in JUnit 4.

By default, the test methods will be executed in the same thread as @AfterEach annotated method.

1. @AfterEach Usage

Annotate a method with @AfterEach as given below:

@AfterEach
public void cleanUpEach(){
	//Test cleanup code
}

@Test
void succeedingTest() {
    //test code and assertions
}

@AfterEach annotated method MUST NOT be a static method otherwise it will throw runtime error.

@AfterEach
public static void cleanUpEach(){
	//Test cleanup code
}

//Error

org.junit.platform.commons.JUnitException: @AfterEach method 'public static void com.howtodoinjava.junit5.examples.JUnit5AnnotationsExample.cleanUpEach()' must not be static.

@AfterEach in parent and child classes

@AfterEach method is inherited from parent classes (or interfaces) as long as they are not hidden or overridden

Furthermore, @AfterEach method from parent classes (or interfaces) will be executed after @AfterEach method in child classes.

2. @AfterEach Annotation Example

Let’s take an example. W have used the Calculator class and added one add method.

We will execute the test 5 times using @RepeatedTest annotation. This annotation will cause the add test to run 5 times.

For each invocation of test method, @AfterEach annotated method should also run once.

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

public class AfterAnnotationsTest {

	@DisplayName("Add operation test")
	@RepeatedTest(5)
	void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo)
	{
		System.out.println("Running test -> " + repetitionInfo.getCurrentRepetition());
		Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");
	}

	@AfterAll
	public static void cleanUp(){
		System.out.println("After All cleanUp() method called");
	}

	@AfterEach
	public void cleanUpEach(){
		System.out.println("After Each cleanUpEach() method called");
	}
}

Where Calculator class is:

package com.howtodoinjava.junit5.examples;

public class Calculator
{
	public int add(int a, int b) {
		return a + b;
	}
}
Running test -> 1
After Each cleanUpEach() method called

Running test -> 2
After Each cleanUpEach() method called

Running test -> 3
After Each cleanUpEach() method called

Running test -> 4
After Each cleanUpEach() method called

Running test -> 5
After Each cleanUpEach() method called

After All cleanUp() method called

Clearly, @AfterEach annotated cleanUpEach() method is called once per test method invocation.

Happy Learning !!

Sourcecode Download

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode