While testing our code, we might come across a scenario where some conditions must be true or false, then only we will continue with tests otherwise not. for that JUnit 5 provides assumptions, if our assumption is true, then only proceed further otherwise aborted.
The assumption class provides the following overloaded methods.
assumeFalse
assumeTrue
assumingThat
package com.example.junit5.sujan;
public class AppleCalculator {
public int addApple(int appleBag1, int appleBag2) {
return appleBag1 + appleBag2;
}
public String getApple() {
return "apple";
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.*;
class AppleCalculatorTest {
@Test
void assumeTrueTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assumeTrue("apple".equalsIgnoreCase(appleCalculator.getApple()));
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
void assumeTrueMessageTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assumeTrue("apple".equalsIgnoreCase(appleCalculator.getApple()), "apple is equal to apple");
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
void assumeTrueLambdaTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assumingThat("apple".equalsIgnoreCase(appleCalculator.getApple()), () -> assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple"));
}
@Test
void assumeFalseTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assumeFalse("oranges".equalsIgnoreCase(appleCalculator.getApple()));
assertEquals(4, appleCalculator.addApple(2, 2), "2 apple + 2 apple is 4 apple");
}
@Test
void assumeFalseMessageTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assumeFalse("oranges".equalsIgnoreCase(appleCalculator.getApple()), "apple is not equal to oranges");
assertEquals(4, appleCalculator.addApple(2, 2), "2 apple + 2 apple is 4 apple");
}
@Test
void assumeFalseLambdaTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assumingThat("orange".equalsIgnoreCase(appleCalculator.getApple()), () -> assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple"));
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}