In this post, we will talk and learn about the JUnit 5 life cycle @BeforeAll annotation.
In JUnit 5 @BeforeAll annotation is used to signal that the annotated method should be executed before all tests in the current test Class.
It is a replacement for @BeforeClass annotation in JUnit 4.
@BeforeAll annotated method must be declared as a static method otherwise it will throw runtime error.
@BeforeAll Annotation Example
Let’s understand @BeforeAll Annotation using an example. Here I have MyUtils.java class with the add method. I will write a Junit test for this add method and we will see how we can make use of @BeforeAll Annotation in the same Unit class.
Utility Class for that we have to write JUnit Test:
1 2 3 4 5 6 7 8 |
package kkjavatutorials.com; public class MyUtils { public int add (int n1,int n2) { return n1+n2; } } |
JUnit Test Class with non static method(setUp()) annotated with @BeforeAll. In this case, JUnit 5 will throw a runtime exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package kkjavatutorials.com; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; class MyUtilsTest { private static MyUtils myUtils; @BeforeAll void setUp(){ System.out.println("Test data set up.."); myUtils = new MyUtils(); } @Test void addTest() { assertEquals(10, myUtils.add(6, 4)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
org.junit.platform.commons.JUnitException: @BeforeAll method 'void kkjavatutorials.com.MyUtilsTest.setUp()' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS). at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.assertStatic(LifecycleMethodUtils.java:57) at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.lambda$findMethodsAndAssertStatic$0(LifecycleMethodUtils.java:81) at java.util.ArrayList.forEach(ArrayList.java:1249) at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080) at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findMethodsAndAssertStatic(LifecycleMethodUtils.java:81) at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findBeforeAllMethods(LifecycleMethodUtils.java:40) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.prepare(ClassBasedTestDescriptor.java:161) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.prepare(ClassBasedTestDescriptor.java:77) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$1(NodeTestTask.java:107) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:107) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:75) at java.util.ArrayList.forEach(ArrayList.java:1249) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90) at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) |
Now in the above test class, we make the setUp() method static then the test case will be executed successfully.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package kkjavatutorials.com; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; class MyUtilsTest { private static MyUtils myUtils; @BeforeAll static void setUp(){ System.out.println("Test data set up.."); myUtils = new MyUtils(); } @Test void addTest() { assertEquals(10, myUtils.add(6, 4)); } } |
The output of the above test class
You May Also Like:
What is JUnit?
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
JUnit 5 @Disabled Test Example
Conditional Test Execution in JUnit
Test Execution Order in Junit 5
JUnit 5 Nested Tests Example
Dependency Injection and Testing in JUnit 5
Test Interfaces and Default Methods in JUnit 5
JUnit 5 @RepeatedTest Annotation example
Junit 5 Parameterized Tests with examples
Argument Conversion in Parameterized Tests in Junit 5
Argument Aggregation in Parameterized Tests in Junit 5
Customizing Display Names of Parameterized Tests in JUnit 5
Dynamic Tests example in Junit 5
JUnit 5 Test Suites Examples
JUnit 5 Temporary Directory Support
How to execute Junit 5 tests in Eclipse IDE
JUnit 5 Test Templates for Eclipse
JUnit 5 vs JUnit 4
That’s all about JUnit 5 @BeforeAll annotation example
If you have any feedback or suggestion please feel free to drop in below comment box.