Execution order of JUnit annotations

The article will help you in understanding the execution order of the methods that use the annotations provided by JUnit 5

Chaanakya
Javarevisited
3 min readJan 12, 2023

--

Basic annotations in JUnit 5

  • BeforeEach
  • BeforeAll
  • AfterEach
  • AfterAll

BeforeEach

The method annotated with BeforeEach executes before each test. We can have multiple BeforeEach methods, but the execution order is not guaranteed.

This is similar to Before annotation in JUnit 4.

AfterEach

The method annotated with AfterEach executes after each test. We can have multiple AfterEach methods, but the execution order is not guaranteed.

This is similar to After annotation in JUnit 4.

BeforeAll

The method annotated with BeforeAll executes once before all the tests. This method has to be static.

This is similar to BeforeClass annotation in JUnit 4.

AfterAll

The method annotated with AfterAll executes once after all the tests. This method has to be static.

This is similar to AfterClass annotation in JUnit 4.

Example

// example test class
public class TestClass {
static {
System.out.println("Static block of Test class");
}

public TestClass() {
System.out.println("Constructor of Test class");
}

@BeforeAll
public static void beforeAll() {
System.out.println("Before All in Test class");
}

@AfterAll
public static void afterAll() {
System.out.println("After All in Test class");
}

@BeforeEach
public void beforeEach() {
System.out.println("Before Each In Test class");
}

@AfterEach
public void afterEach() {
System.out.println("After Each in Test class");
}

@Test
public void testOne() {
System.out.println("Test 1 in Test class");
}
}

/*
Output:

Static block of Test class
Before All in Test class
Constructor of Test class
Before Each In Test class
Test 1 in Test class
After Each in Test class
After All in Test class
*/

The order in which these methods run for a test execution:

The order in which the methods run when the test class extends a base class:

Although I’m not really inclined towards this approach, I’ve seen it being used to avoid code duplication for setup across multiple test classes.

The following example should help you understand the order in which these methods execute when inheritance comes into picture.

public class ParentClass {
static {
System.out.println("Static block of Parent class");
}

public ParentClass() {
System.out.println("Constructor of Parent class");
}

@BeforeAll
public static void beforeAllInParent() {
System.out.println("Before All in Parent class");
}

@AfterAll
public static void afterAllInParent() {
System.out.println("After All in Parent class");
}

@BeforeEach
public void beforeEachInParent() {
System.out.println("Before Each in Parent class");
}

@AfterEach
public void afterEachInParent() {
System.out.println("After Each in Parent class");
}
}
public class TestClass extends ParentClass {
static {
System.out.println("Static block of Test class");
}

public TestClass() {
System.out.println("Constructor of Test class");
}

@BeforeAll
public static void beforeAll() {
System.out.println("Before All in Test class");
}

@AfterAll
public static void afterAll() {
System.out.println("After All in Test class");
}

@BeforeEach
public void beforeEach() {
System.out.println("Before Each In Test class");
}

@AfterEach
public void afterEach() {
System.out.println("After Each in Test class");
}

@Test
public void testOne() {
System.out.println("Test 1 in Test class");
}
}

/*
Output:

Static block of Parent class
Before All in Parent class
Static block of Test class
Before All in Test class
Constructor of Parent class
Constructor of Test class
Before Each in Parent class
Before Each In Test class
Test 1 in Test class
After Each in Test class
After Each in Parent class
After All in Test class
After All in Parent class
*/

The order in which these methods run:

That’s all for now. Feel free to drop any comments or questions you have.
Happy Coding!

--

--