@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