Thursday 1 July 2021

Junit5: @EnabledOnJre, @DisabledOnJre : Execute test cases based on jre version

@EnabledOnJre and @DisabledOnJre annotations are used to enable or disable test methods.

 

Example 1: Enable test only on JAVA_8

@Test
@EnabledOnJre(JAVA_8)
void onlyOnJava8() {
  assertTrue(true);
}

 

Example 2: Enable test on JAVA_8 or JAVA_9 or JAVA_10

@Test
@EnabledOnJre({ JAVA_8, JAVA_9, JAVA_10 })
void onJava8or9Or10() {
  assertTrue(true);
}

 

Example 3: Disable test on JAVA_8

@Test
@DisabledOnJre(JAVA_8)
void notOnJava8() {
  assertTrue(true);
}

 

JRESpecificTests.java

package com.sample.app;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.EnabledOnJre;

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

public class JRESpecificTests {
  @Test
  @EnabledOnJre(JAVA_8)
  void onlyOnJava8() {
    assertTrue(true);
  }

  @Test
  @EnabledOnJre({ JAVA_8, JAVA_9, JAVA_10 })
  void onJava8or9Or10() {
    assertTrue(true);
  }

  @Test
  @DisabledOnJre(JAVA_8)
  void notOnJava8() {
    assertTrue(true);
  }
}

 

Run above test class, you can observe test methods may execute or skipped depends on your JRE version.

 

When I ran the test method on Jdk 1.8, ‘notOnJava8’ method is skipped.

 


 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment