Sunday 11 July 2021

Junit5: AfterAll: Execute after all the tests get executed

@AfterAll annotation is used to signal that the annotated method should be executed after all tests in the current test class.

 

@AfterAll method constraints

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

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

 

Is @AfterAll methods take parameters?

Yes, @AfterAll method may optionally declare parameters to be resolved by ParameterResolvers.

 

AfterAllDemo1.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 AfterAllDemo1 {
  
  @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 AfterAllDemo1 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 @AfterAll methods will work with Inheritance?

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

 

BaseAfterAllMethods.java

package com.sample.app;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

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

  @AfterAll
  static void cleanupSystem() {
    System.out.println("Cleanup system resources");
  }
}

 

LoginImplTest.java

package com.sample.app;

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

public class LoginImplTest extends BaseAfterAllMethods{
  @BeforeAll
  static 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");
  }

  @AfterAll
  static void deleteCreds() {
    System.out.println("Delete user credentials");
  }

}

 

Run LoginImplTest, you will see below messages in console.

Inside setup system
Setting up user credentials
Inside login test

Inside logout test

Delete user credentials
Cleanup system resources

 

Is there any execution order guarantee for @BeforeAll methods?

JUnit Jupiter does not guarantee the execution order of multiple @AfterAll 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