Conditional test execution

In order to establish custom conditions for test execution, we need to use the JUnit 5 extension model (introduced in Chapter 2, What's new in JUnit 5, in the section The extension model of JUnit 5). Concretely, we need to use the conditional extension point called ExecutionCondition. This extension can be used to deactivate either all tests in a class or individual tests.

We are going to see a working example in which we create a custom annotation to disable tests based on the operative system. First of all, we create a custom utility enumeration to select one operative system (WINDOWS, MAC, LINUX, and OTHER):

package io.github.bonigarcia;

public enum Os {
WINDOWS, MAC, LINUX, OTHER;

public static Os determine() {
Os out = OTHER;
String myOs = System.getProperty("os.name").toLowerCase();
if (myOs.contains("win")) {
out = WINDOWS;
}
else if (myOs.contains("mac")) {
out = MAC;
}
else if (myOs.contains("nux")) {
out = LINUX;
}
return out;
}
}

Then, we create an extension of ExecutionCondition. In this example, the evaluation is done by checking whether or not the custom annotation @DisabledOnOs is present. When the annotation @DisabledOnOs is present, the value of the operative system is compared with the current platform. Depending on the result of that condition, the test is disabled or enabled.

package io.github.bonigarcia;

import java.lang.reflect.AnnotatedElement;
import java.util.Arrays;
import java.util.Optional;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.util.AnnotationUtils;

public class OsCondition implements ExecutionCondition {

@Override
public ConditionEvaluationResult evaluateExecutionCondition(
ExtensionContext context) {
Optional<AnnotatedElement> element = context.getElement();
ConditionEvaluationResult out = ConditionEvaluationResult
.enabled("@DisabledOnOs is not present");
Optional<DisabledOnOs> disabledOnOs = AnnotationUtils
.findAnnotation(element, DisabledOnOs.class);
if (disabledOnOs.isPresent()) {
Os myOs = Os.determine();
if(Arrays.asList(disabledOnOs.get().value())
.contains(myOs)) {
out = ConditionEvaluationResult
.disabled("Test is disabled on " + myOs);
}
else {
out = ConditionEvaluationResult
.enabled("Test is not disabled on " + myOs);
}
}
System.out.println("--> " + out.getReason().get());
return out;
}

}

Moreover, we need to create our custom annotation @DisabledOnOs, which is also annotated with @ExtendWith pointing to our extension point.

package io.github.bonigarcia;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(OsCondition.class)
public @interface DisabledOnOs {
Os[] value();
}

Finally, we use our annotation @DisabledOnOs in a Jupiter test.

import org.junit.jupiter.api.Test;

import static io.github.bonigarcia.Os.MAC;
import static io.github.bonigarcia.Os.LINUX;

class DisabledOnOsTest {

@DisabledOnOs({ MAC, LINUX })
@Test
void conditionalTest() {
System.out.println("This test will be disabled on MAC and LINUX");
}

}

If we execute this test in a Windows machine, the test is not skipped, as we can see in this snapshot:

Execution of conditional test example
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.122.68