Junit 5 Test Class Lifecycle @AfterAll

After running the test cases, we need to end the execution environment, destructing the objects, closing connections, etc. For this purpose JUnit, 5 Provides @AfterAll annotation. The method annotated with @AfterAll annotation will be executed only once and after all the tests in the current test class.

JUnit 4 @AfterClass = JUnit 5 @AfterAll

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.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class AppleCalculatorTest {
    @AfterAll
    static void afterAll() {
        System.out.println("AppleCalculatorTest.afterAll");
    }

    @Test
    void test() {
        System.out.println("AppleCalculatorTest.test");
        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 @AfterAll must have a void return type, it should be static or default. @AfterAll methods must not be private.

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.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class AppleCalculatorTest {
    @AfterAll
    static String afterAllCase1() {
        System.out.println("AppleCalculatorTest.afterAllCase1 has String return type");
        return "apple";
    }

    @AfterAll
    private static void afterAllCase2() {
        System.out.println("AppleCalculatorTest.afterAllCase2 has private access modifier");
    }

    @AfterAll
    private void afterAllCase3() {
        System.out.println("AppleCalculatorTest.afterAllCase3 has not static access modifier");
    }

    @Test
    void test() {
        System.out.println("AppleCalculatorTest.test");
        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 @BeforeAll 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.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class AppleCalculatorTest {
    @AfterAll
    static void AfterAllCase3() {
        System.out.println("AppleCalculatorTest.AfterAllCase3");
    }

    @AfterAll
    static void AfterAllCase2() {
        System.out.println("AppleCalculatorTest.AfterAllCase2");
    }

    @AfterAll
    static void AfterAllCase1() {
        System.out.println("AppleCalculatorTest.AfterAllCase1");
    }

    @Test
    void test() {
        System.out.println("AppleCalculatorTest.test");
        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