JUnit 5 @BeforeAll

JUnit 5 @BeforeAll annotation denotes a method that it is a lifecycle method. @BeforeAll is the replacement of @BeforeClass annotation in JUnit 4.

1. @BeforeAll Annotation

@BoforeAll is used to signal that the annotated method should be executed before all the @Test@RepeatedTest@ParameterizedTest, and @TestFactory methods in the current class.

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

  • @BeforeAll annotated method MUST be a static method in the test class.
@BeforeAll
public static void init(){
	System.out.println("BeforeAll init() method called");
}
  • Or, we can apply this annotation on interface default methods if the test interface or test class is annotated with @TestInstance(Lifecycle.PER_CLASS)
@TestInstance(Lifecycle.PER_CLASS)
interface TestLifecycleLogger {

    @BeforeAll
    default void beforeAllTests() {
        //
    }
}

If failed to do so, JUnit will throw runtime error of type JUnitException.

org.junit.platform.commons.JUnitException: 
@BeforeAll method 'public void com.howtodoinjava.junit5.examples. 
JUnit5AnnotationsExample.init()' must be static.
at org.junit.jupiter.engine.descriptor. 
LifecycleMethodUtils.assertStatic(LifecycleMethodUtils.java:66)

@BeforeAll in parent and child classes

@BeforeAll methods are inherited from parent classes (or interfaces) as long as they are not hidden or overridden

Furthermore, @BeforeAll methods from parent classes (or interfaces) will be executed before @BeforeAll methods in child classes.

2. @BeforeAll Annotation Example

Let’s take an example. We have written a test for the Calculator class and its add() method.

We will execute the test 5 times using @RepeatedTest annotation. @Repeated annotation will cause the add test to run five times. But @BeforeAll annotated method must be called only once.

package com.howtodoinjava.junit5.examples;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
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 BeforeAllTest {

	@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");
	}

	@BeforeAll
	public static void init(){
		System.out.println("Before All init() method called");
	}
}

Where Calculator class is:

package com.howtodoinjava.junit5.examples;

public class Calculator
{
	public static int add(int a, int b) {
		return a + b;
	}
}

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

Before All init() method called

Running test -> 1
Running test -> 2
Running test -> 3
Running test -> 4
Running test -> 5

Clearly, @BeforeAll annotated init() method is called only once.

Happy Learning !!

Sourcecode Download

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

1 thought on “JUnit 5 @BeforeAll”

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.