Thursday 1 July 2021

Junit5: EnabledIfSystemProperty, DisabledIfSystemProperty: Enable or disable test cases based on system property

@EnabledIfSystemProperty annotation is used to signal that the annotated test class or test method is only enabled if the value of the specified system property matches the specified regular expression.

 

Example

@Test
@EnabledIfSystemProperty(named = "envt", matches = "staging")
void onlyOnStaging() {
    assertTrue(true);
}

 

@DisabledIfSystemProperty annotation is used to signal that the annotated test class or test method is disabled if the value of the specified system property matches the specified regular expression.

 

Example

 

@Test
@DisabledIfSystemProperty(named = "envt", matches = "staging")
void notOnStaging() {
   assertTrue(true);
}

 

SystemPropertyConditionTest.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

public class SystemPropertyConditionTest {
  
  @BeforeAll
  static void setSystemProperties() {
    System.setProperty("envt", "staging");
  }
  
  @Test
  @EnabledIfSystemProperty(named = "envt", matches = "staging")
  void onlyOnStaging() {
      assertTrue(true);
  }

  @Test
  @DisabledIfSystemProperty(named = "envt", matches = "staging")
  void notOnStaging() {
     assertTrue(true);
  }
}

 
Run this test class, you will see ‘notOnStaging’ test method will not get executed.

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment