Before starting the test cases, we need to set up the necessary environment, initiating the objects, creating connections, etc. For this purpose Junit, 5 Provides @BeforeAll
annotation. The method annotated with @BeforeAll
annotation will be executed only once and before all the tests in the current test class.
JUnit 4
@BeforeClass
= JUnit 5@BeforeAll
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.BeforeAll;
import org.junit.jupiter.api.Test;
class AppleCalculatorTest {
@BeforeAll
static void beforeAll() {
System.out.println("AppleCalculatorTest.beforeAll");
}
@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 @BeforeAll
must have a void return type, it should be static or default. @BeforeAll
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.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class AppleCalculatorTest {
@BeforeAll
static String beforeAllCase1() {
System.out.println("AppleCalculatorTest.beforeAllCase1 has String return type");
return "apple";
}
@BeforeAll
private static void beforeAllCase2() {
System.out.println("AppleCalculatorTest.beforeAllCase2 has private access modifier");
}
@BeforeAll
private void beforeAllCase3() {
System.out.println("AppleCalculatorTest.beforeAllCase3 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')
}
test {
useJUnitPlatform()
}
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.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class AppleCalculatorTest {
@BeforeAll
static void beforeAllCase3() {
System.out.println("AppleCalculatorTest.beforeAllCase3");
}
@BeforeAll
static void beforeAllCase2() {
System.out.println("AppleCalculatorTest.beforeAllCase2");
}
@BeforeAll
static void beforeAllCase1() {
System.out.println("AppleCalculatorTest.beforeAllCase1");
}
@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')
}