Junit 5 Java Runtime Environment Conditions

test cases can be enable and disable based on Java Runtime Envirmnet conditions.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) {
        return appleBag1 + appleBag2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.condition.JRE.*;

class AppleCalculatorTest {

    @Test
    @EnabledOnJre(JRE.JAVA_14)
    void addAppleEnabledOnJreTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    @DisabledOnJre(JRE.JAVA_8)
    void addAppleDisabledOnJreTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    @EnabledOnJre({JRE.JAVA_13, JRE.JAVA_14})
    void addAppleEnabledOnMultipleJreTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    @DisabledOnJre({JRE.JAVA_8, JAVA_9})
    void addAppleDisabledOnMultipleJreTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    @EnabledForJreRange(min = JAVA_13, max = JAVA_15)
    void addAppleEnabledOnJreRangeTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    @DisabledForJreRange(min = JAVA_8, max = JAVA_11)
    void addAppleDisabledOnJreRangeTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        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')
}

 

follow us on