Junit 5 Test Class Lifecycle @AfterEach

After executing each test case, we need to run a few codes for destructing object data from the last test case of re-initializing variables with the default value, etc For this purpose JUnit, 5 Provides @AfterEach annotation. The method annotated with @AfterEach annotation will be executed every time after executing a test case.

JUnit 4 @After = JUnit 5 @AfterEach

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }
}
package com.example.junit5.sujan;

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

public class AppleCalculatorTest {
    @AfterEach
    void setUp() {
        System.out.println("AppleCalculatorTest.setUp");
    }

    @Test
    void addApple() {
        System.out.println("AddAppleCalculatorTest.addApple");
        AppleCalculator appleCalculator = new AppleCalculator();
        Assertions.assertEquals(2, appleCalculator.addApple(1, 1));
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

methods annotated with @AfterEach must have a void return type. methods must not be private and must not be static. if required they can have ParameterResolver as the parameter.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }
}
package com.example.junit5.sujan;

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

public class AppleCalculatorTest {
    @AfterEach
    static void setUpCase3() {
        System.out.println("AppleCalculatorTest.setUpCase3");
    }

    @AfterEach
    String setUpCase1() {
        System.out.println("AppleCalculatorTest.setUpCase1");
        return "apple";
    }

    @AfterEach
    private void setUpCase2() {
        System.out.println("AppleCalculatorTest.setUpCase2");
    }

    @Test
    void addApple() {
        System.out.println("AddAppleCalculatorTest.addApple");
        AppleCalculator appleCalculator = new AppleCalculator();
        Assertions.assertEquals(2, appleCalculator.addApple(1, 1));
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

if multiple methods are annotated with @AfterEach declared in a single class or interface, then their order is not alphabetical, it might occur that every time they are running in alphabetical order but its not guaranteed.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }
}
package com.example.junit5.sujan;

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

public class AppleCalculatorTest {
    @AfterEach
    void setUpCase3() {
        System.out.println("AppleCalculatorTest.setUpCase3");
    }

    @AfterEach
    void setUpCase2() {
        System.out.println("AppleCalculatorTest.setUpCase2");
    }

    @AfterEach
    void setUpCase1() {
        System.out.println("AppleCalculatorTest.setUpCase1");
    }

    @Test
    void addApple() {
        System.out.println("AddAppleCalculatorTest.addApple");
        AppleCalculator appleCalculator = new AppleCalculator();
        Assertions.assertEquals(2, appleCalculator.addApple(1, 1));
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

follow us on