Junit 5 Test Class Lifecycle @BeforeEach

Before executing each test case, we need to run a few codes for clearing object data from the last test case of reinitializing variables with the default value, etc For this purpose Junit, 5 Provides @BeforeEach annotation. The method annotated with @BeforeEach annotation will be executed every time before executing the test case.

JUnit 4 @Before = JUnit 5 @BeforeEach

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }

    public int subApple(int apple1, int apple2) {
        return apple1 - apple2;
    }
}
package com.example.junit5.sujan;

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

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

    @Test
    void addApple() {
        System.out.println("AppleCalculatorTest.addApple");
        AppleCalculator appleCalculator = new AppleCalculator();
        Assertions.assertEquals(2, appleCalculator.addApple(1, 1));
    }

    @Test
    void subApple() {
        System.out.println("AppleCalculatorTest.subApple");
        AppleCalculator appleCalculator = new AppleCalculator();
        Assertions.assertEquals(1, appleCalculator.subApple(2, 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 @BeforeEach must have a void return type. methods must not be private and must not be static. if required they can have ParameterResolver as a 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.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

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

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

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

public class AppleCalculatorTest {

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

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

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

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