Sunday 11 July 2021

Junit5: AfterEach: Execute this method after every test

@AfterEach is used to signal that the annotated method should be executed after each @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, and @TestTemplate method in the current test class.

 

Constraints on @AfterEach method

@AfterEach methods must have a void return type, must not be private, and must not be static.

 

Can an @AfterEach method take parameters?

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

 

Let’s see it with an example.

 

AfterEachMethodDemo1.java

package com.sample.app;

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

public class AfterEachMethodDemo1 {
  @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");
  }
}

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

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


Inheritance and Execution Order

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

 

BaseAfterEachMethods.java

package com.sample.app;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

public class BaseAfterEachMethods {

  @BeforeEach
  void setupSystem() {
    System.out.println("Setting up the system");
  }

  @BeforeEach
  void setupLogger() {
    System.out.println("Setting up logger");
  }

  @AfterEach
  void cleanupSystem() {
    System.out.println("Cleaning the system\n");
  }

  @AfterEach
  void cleanupLogger() {
    System.out.println("Cleaning logger");
  }
}


LoginServiceTest.java

package com.sample.app;

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

public class LoginServiceTest extends BaseAfterEachMethods {
  @BeforeEach
  void setupCreds() {
    System.out.println("Setting up user credentials");
  }

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

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

  @AfterEach
  void deleteCreds() {
    System.out.println("Deleting Credentials");
  }
}


Run LoginServiceTest, you will see below messages in console.

Setting up logger
Setting up the system
Setting up user credentials
Inside login test
Deleting Credentials
Cleaning logger
Cleaning the system

Setting up logger
Setting up the system
Setting up user credentials
Inside logout test
Deleting Credentials
Cleaning logger
Cleaning the system


Is there any execution order guarantee for @AfterEach methods?

JUnit does not guarantee the execution order of multiple @AfterEach methods that are declared within a single test class or test interface. So, it is recommended to declare at most one @BeforeEach method and at most one @AfterEach method per test class or test interface unless there are no dependencies between the @BeforeEach methods or between the @AfterEach methods.

 

You can download all the applications from this link.

https://github.com/harikrishna553/junit5/tree/master/junit5-examples

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment