Junit 5 Test Class Lifecycle

A JUnit 5 test case needs to get through multiple life cycles. Primarily there is 4 major life cycle of Junit 5 ie @BeforeAll, @BeforeEach, @AfterEach and @AfterAll.

package org.wesome.junit5;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }
}
package org.wesome.junit5;

import org.junit.jupiter.api.*;

class AppleCalculatorTest {
    @BeforeAll
    static void beforeAll() {
        System.out.println("AppleCalculatorTest.beforeAll");
    }

    @BeforeEach
    void beforeEach() {
        System.out.println("AppleCalculatorTest.beforeEach");
    }

    @Test
    void test() {
        System.out.println("AppleCalculatorTest.test");
    }

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

   @AfterAll
    static void afterAll() {
        System.out.println("AppleCalculatorTest.afterAll");
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}

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

 

follow us on