HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions

JUnit 5 @AfterEach Annotation Example

By Lokesh Gupta | Filed Under: JUnit 5

JUnit 5 @AfterEach annotation is replacement of @After annotation in JUnit 4. It is used to signal that the annotated method should be executed after each @Test method in the current class.

@AfterEach Annotation Usage

Annotate a method with @AfterEach as given below:

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

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

org.junit.platform.commons.JUnitException: @AfterEach method 'public static void com.howtodoinjava.junit5.examples.JUnit5AnnotationsExample.cleanUpEach()' must not be static.
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.assertNonStatic(LifecycleMethodUtils.java:73)
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.lambda$findAfterEachMethods$3(LifecycleMethodUtils.java:60)
	at java.util.ArrayList.forEach(ArrayList.java:1249)
	at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080)
	at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findAfterEachMethods(LifecycleMethodUtils.java:60)

@AfterEach Annotation Example

Let’s take an example. I have used one Calculator class and added one add method. I will test it 5 times using @RepeatedTest annotation. This annotation will cause the add test to run 5 times. For every run of test method, @AfterEach annotated method should also run every time.

package com.howtodoinjava.junit5.examples;

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.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
public class JUnit5AnnotationsExample {

	@Test
	@DisplayName("Add operation test")
	@RepeatedTest(5)
	void addNumber(TestInfo testInfo) {
		Calculator calculator = new Calculator();
		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;
	}
}

Now execute the test and you will see below console output:

After Each cleanUpEach() method called
After Each cleanUpEach() method called
After Each cleanUpEach() method called
After Each cleanUpEach() method called
After Each cleanUpEach() method called
After Each cleanUpEach() method called
After All cleanUp() method called

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

Happy Learning !!

About Lokesh Gupta

Founded HowToDoInJava.com in late 2012. I love computers, programming and solving problems everyday. A family guy with fun loving nature. You can find me on Facebook, Twitter and Google Plus.

Ask Questions & Share Feedback Cancel reply

Your email address will not be published. Required fields are marked *

*Want to Post Code Snippets or XML content? Please use [java] ... [/java] tags otherwise code may not appear partially or even fully. e.g.
[java] 
public static void main (String[] args) {
...
}
[/java]

Search Tutorials

  • Email
  • Facebook
  • RSS
  • Twitter

JUnit 5 Tutorial

  • JUnit 5 – Introduction
  • JUnit 5 – Test LifeCycle
  • JUnit 5 – @BeforeAll
  • JUnit 5 – @BeforeEach
  • JUnit 5 – @AfterEach
  • JUnit 5 – @AfterAll
  • JUnit 5 – @RepeatedTest
  • JUnit 5 – @Disabled
  • JUnit 5 – @Tag
  • JUnit 5 – Expected Exception
  • JUnit 5 – Assertions Examples
  • JUnit 5 – Assumptions
  • JUnit 5 – Test Suites
  • JUnit 5 – Gradle Dependency
  • JUnit 5 – Maven Dependency
  • JUnit 5 – Execute Test in Eclipse
  • JUnit 5 – Eclipse Test Templates
  • JUnit 5 vs JUnit 4

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Java Collections
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap