Thursday 1 July 2021

Junit5: @EnabledForJreRange, @DisabledForJreRange: Enable or disable based on Jre

@EnabledForJreRange: Enable test method or test class for a specific range of Java Runtime Environment versions. When applied at the class level, all test methods within that class will be enabled on the same specified JRE versions.

 

@DisabledForJreRange: Disable test method or test class for a specific range of Java Runtime Environment versions. When applied at the class level, all test methods within that class will be enabled on the same specified JRE versions.

 

Example

@DisabledForJreRange(min = JAVA_9, max = JAVA_11)
void notFromJava9to11() {

}

 

Find the below working application.

 

JRESpecificRangeTests.java

 

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.condition.JRE.JAVA_11;
import static org.junit.jupiter.api.condition.JRE.JAVA_9;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledForJreRange;
import org.junit.jupiter.api.condition.EnabledForJreRange;

public class JRESpecificRangeTests {

  @Test
  @EnabledForJreRange(min = JAVA_9, max = JAVA_11)
  void fromJava9to11() {
    assertTrue(true);
  }

  @Test
  @EnabledForJreRange(min = JAVA_9)
  void fromJava9toCurrentJavaFeatureNumber() {
    assertTrue(true);
  }

  @Test
  @EnabledForJreRange(max = JAVA_11)
  void fromJava8To11() {
    assertTrue(true);
  }

  @Test
  @DisabledForJreRange(min = JAVA_9, max = JAVA_11)
  void notFromJava9to11() {
    assertTrue(true);
  }

  @Test
  @DisabledForJreRange(min = JAVA_9)
  void notFromJava9toCurrentJavaFeatureNumber() {
    assertTrue(true);
  }

  @Test
  @DisabledForJreRange(max = JAVA_11)
  void notFromJava8to11() {
    assertTrue(true);
  }
}

When I ran the test class on Java8, following test methods skipped.

a.   notFromJava8to11

b.   fromJava9toCurrentJavaFeatureNumber

c.    fromJava9to11

 


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment