JUnit 5 is the recent version of the JUnit testing framework. JUnit 5 is the most powerful version compared to the other versions because it consists of enhanced features that provide the foundation for developer-side testing. Moreover, writing and running the unit tests with the JUnit 5 testing framework increases the correctness of your code and ensures your Java application has no defects.
In JUnit 5, the @AfterEach annotation is just as same as the @After annotation which is in JUnit 4 testing framework. The only difference is that the naming convention between these both. If your JUnit testing framework is Junit4 then go for @After Annotation and JUnit 5 @AfterEach Annotation.
Prerequisites
- Knowledge of Java Programming Language.
- Basic understanding of Junit testing.
- Hands-on experience with Eclipse IDE.
Setting up Junit 5 Jar Files
First, we will write test cases in a simple Java project. To make sure that JUnit works finely in our program
- Download the Junit5 jar files from this link.
- Import them into the IDE. To import the jar files into the Java project.
- Right-click on your Java Project > Select the option "Buildpath" > Select "Add libraries". Then import the jar file from the local computer into the libraries.
@AfterEach Annotation
@AfterEach annotation is generally used to indicate that the annotated method must be executed after every @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory in the same Test class. This means whenever you run the test method, the @AfterEach method will be executed at least once after the test method execution.
The method which annotated by the @AfterEach annotation should be non static and it should be void return type.
Example of @AfterEach annotation
Now, let's go through an example to understand more about @AfterEach annotation.
Project Structure:

Main Class:
Java
package com.application.logic;
public class Program {
public int findMult(int a,int b)
{
return a*b;
}
}
In the above program, we have created a class with name "Program" with a method "findMult" inside. This method calculates and returns the product of two integers. We will be testing the above code with Junit and we are implementing the @AfterEach annotation and find out the functionality of it.
Test Class:
Java
package com.application;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.TestInfo;
import com.application.logic.Program;
public class TestCode {
@RepeatedTest(3)
public void testFindMult(TestInfo testInfo, RepetitionInfo repetitionInfo)
{
System.out.println("Test Running: "+repetitionInfo.getCurrentRepetition());
Program obj=new Program();
assertEquals(8,obj.findMult(4,2));
}
@AfterEach
public void cleanCheck()
{
System.out.println("Running in CleanCheck ");
System.out.println("---------------------------------------------------------");
}
}
Output:
Test Running: 1
Running in CleanCheck
---------------------------------------------------------
Test Running: 2
Running in CleanCheck
---------------------------------------------------------
Test Running: 3
Running in CleanCheck
---------------------------------------------------------
Explaination of the above Method:
- In the above program we have created a class TestCode for testing the findMult method. Inside this we have created a test method testFindMult() which validates our test case.
- @RepeatedTest annotation is used to iterate the test case method for 3 times so that we can know how the @AfterEach annotated method is working.
- (TestInfo testInfo, RepetitionInfo repetitionInfo) parameters are passed to this method to get the count of iteration.
- testFindMult() method is the test case where it actually tests through assertEquals by passing two integers 2 and 4 to the findMul() method which is in the "Program" class and tests the output of two integers whether it is equal to 8 or not.
- cleanCheck() method is annotated with @AfterEach annotation. This method is executed each time whenever the "testFindMult" method is tested at least once.
Note: The @AfterEach annotated method should never be a static method. This will throw a runtime error
In the above output, we can see that whenever the "testFindMult" method is executed parallelly @AfterEach annotated method "cleanCheck()" is also executed. which means after executing the testFindMult() method cleanCheck() method called and "Running in CleanCheck " is printed right after it.
Conclusion
@AfterEach annotation enhances the test isolation whenever each iteration is tested. This helps developers to maintain and run their tests without any difficulties. This annotaion helps to reduce the redundant code in testing. Test readability is increased with this annotation. The Junit 5 @AfterEach annotation serves the same functionality as @After annotation which is in JUnit 4 testing framework. The only difference is the naming convention and the version difference of the testing framework.