Sunday 11 July 2021

Junit5: BeforeAll: Execute this method before all tests in current class

@BeforeAll annotation is used to signal that the annotated method should be executed before all tests in the current test class. @BeforeAll methods are only executed once for a given test class.

 

Constraints on @BeforeAll method

a. @BeforeAll methods must have a void return type, must not be private, and must be static by default.

 

b. @BeforeAll methods are not supported in @Nested test classes or as interface default methods unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).

 

Can a @BeforeAll method takes any parameters?

Yes, @BeforeAll methods may optionally declare parameters to be resolved by ParameterResolvers.

 

BeforeAllDemo1.java

package com.sample.app;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class BeforeAllDemo1 {
  
  @BeforeAll
  static void beforeAll_1() {
    System.out.println("Inside beforeAll_1\n");
  }
  
  @BeforeAll
  static void beforeAll_2() {
    System.out.println("Inside beforeAll_2\n");
  }
  
  @BeforeEach
  void beforeEach_1() {
    System.out.println("\nInside beforeEach_1");
  }

  @BeforeEach
  void beforeEach_2() {
    System.out.println("Inside beforeEach_2");
  }

  @Test
  public void test1() {
    System.out.println("Inside test1");
  }

  @Test
  public void test2() {
    System.out.println("Inside test2");
  }

  @AfterEach
  void afterEach_1() {
    System.out.println("Inside afterEach_1");
  }

  @AfterEach
  void afterEach_2() {
    System.out.println("Inside afterEach_2");
  }
  
  @AfterAll
  static void afterAll_1() {
    System.out.println("\nInside afterAll_1");
  }
  
  @AfterAll
  static void afterAll_2() {
    System.out.println("\nInside afterAll_2");
  }
}

 

Run above application, you will see below messages in console.

Inside beforeAll_1

Inside beforeAll_2


Inside beforeEach_1
Inside beforeEach_2
Inside test1
Inside afterEach_1
Inside afterEach_2

Inside beforeEach_1
Inside beforeEach_2
Inside test2
Inside afterEach_1
Inside afterEach_2

Inside afterAll_1

Inside afterAll_2

 

How @BeforeAll methods will work with Inheritance?

@BeforeAll methods are inherited from superclasses as long as they are not hidden or overridden. Furthermore, @BeforeAll methods from superclasses will be executed before @BeforeAll methods in subclasses.

 

BaseBeforeAllMethods.java

package com.sample.app;

import org.junit.jupiter.api.BeforeAll;

public class BaseBeforeAllMethods {
  
  @BeforeAll
  static void setupSystem() {
    System.out.println("Inside setup system");
  }

}

 

LoginServTest.java

package com.sample.app;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class LoginServTest extends BaseBeforeAllMethods {

  @BeforeAll
  static void setupLogger() {
    System.out.println("Inside setup logger\n");
  }

  @BeforeEach
  void setupCreds() {
    System.out.println("Setting up user credentials");
  }

  @Test
  public void loginTest() {
    System.out.println("Inside login test\n");
  }

  @Test
  public void logoutTest() {
    System.out.println("Inside logout test\n");
  }
}

 

Run ‘LoginServTest’ class, you will see below messages in console.

Inside setup system
Inside setup logger

Setting up user credentials
Inside login test

Setting up user credentials
Inside logout test

 

Is there any execution order guarantee for @BeforeAll methods?

JUnit Jupiter does not guarantee the execution order of multiple @BeforeAll methods that are declared within a single test class or test interface. While it may at times appear that these methods are invoked in alphabetical order, they are in fact sorted using an algorithm that is deterministic but intentionally non-obvious.

 

So it is recommended to have at most one @BeforeAll method and at most one @AfterAll method per test class or test interface unless there are no dependencies between the @BeforeAll methods or between the @AfterAll methods.

 

 


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment