...
/System Property Conditions - @DisabledIfSystemProperty and @EnabledIfSystemProperty
System Property Conditions - @DisabledIfSystemProperty and @EnabledIfSystemProperty
This lesson demonstrates how to disable or enable test methods or a complete test class using System property level conditions.
We'll cover the following...
@DisabledIfSystemProperty and @EnabledIfSystemProperty
Junit 5 helps us to disable or enable test cases using various conditions. JUnit Jupiter API provides annotations in org.junit.jupiter.api.condition
package to enable/disable tests based on a certain condition. The annotations provided by API can be applied to test methods as well as the class itself.
The two annotations which use system properties and specified regex to disable or enable tests are - @DisabledIfSystemProperty
and @EnabledIfSystemProperty
. Let’s take a look at a demo.
package com.hubberspot.junit5.disabled;import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertTrue;import org.junit.jupiter.api.Test;import org.junit.jupiter.api.condition.DisabledIfSystemProperty;public class DisabledIfSystemPropertyTest {@Testvoid testOnAllSystemProperties() {assertTrue(3 > 0);}@DisabledIfSystemProperty(named="user.name", matches="dinesh")@Testvoid testDisabledIfUserNameMatchesDinesh() {assertFalse(0 > 4);}@DisabledIfSystemProperty(named="os.name", matches="Windows")@Testvoid testDisabledIfOperatingSystemMatchesWindows() {assertFalse(10 > 40);}}
Above test program has 3 test methods and @DisabledIfSystemProperty
is applied on 2 test methods as.
testDisabledIfUserNameMatchesDinesh()
- Here,@DisabledIfSystemProperty