Java Code Examples for org.junit.jupiter.api.extension.ExtensionContext#getElement()

The following examples show how to use org.junit.jupiter.api.extension.ExtensionContext#getElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DisabledOnNativeImageCondition.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Containers/tests are disabled if {@code @DisabledOnNativeImage} is present on the test
 * class or method and we're running on a native image.
 */
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    Optional<AnnotatedElement> element = context.getElement();
    Optional<DisabledOnNativeImage> disabled = findAnnotation(element, DisabledOnNativeImage.class);
    if (disabled.isPresent()) {
        // Cannot use ExtensionState here because this condition needs to be evaluated before QuarkusTestExtension
        boolean nativeImage = findAnnotation(context.getTestClass(), NativeImageTest.class).isPresent();
        if (nativeImage) {
            String reason = disabled.map(DisabledOnNativeImage::value)
                    .filter(StringUtils::isNotBlank)
                    .orElseGet(() -> element.get() + " is @DisabledOnNativeImage");
            return ConditionEvaluationResult.disabled(reason);
        }
    }
    return ENABLED;
}
 
Example 2
Source File: OsCondition.java    From mastering-junit5 with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: OsCondition.java    From Mastering-Software-Testing-with-JUnit-5 with MIT License 6 votes vote down vote up
@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;
}
 
Example 4
Source File: CapabilityCondition.java    From status-keycard with Apache License 2.0 6 votes vote down vote up
/**
 * Containers/tests are disabled if {@code @Disabled} is present on the test
 * class or method.
 */
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
  Optional<AnnotatedElement> element = context.getElement();
  Optional<Capabilities> capsAnnotation = findAnnotation(element, Capabilities.class);

  if (capsAnnotation.isPresent()) {
    for (String c : capsAnnotation.get().value()) {
      if (!availableCapabilities.contains(c)) {
        return disabled("The " + c + " capability is not available on the tested target");
      }
    }

    return ENABLED;
  }

  return ENABLED_BY_DEFAULT;
}
 
Example 5
Source File: GuiceExtension.java    From bobcat with Apache License 2.0 6 votes vote down vote up
/**
 * Create {@link Injector} or get existing one from test context
 */
private static Optional<Injector> getOrCreateInjector(ExtensionContext context)
    throws NoSuchMethodException, InstantiationException, IllegalAccessException,
    InvocationTargetException {

  Optional<AnnotatedElement> optionalAnnotatedElement = context.getElement();
  if (!optionalAnnotatedElement.isPresent()) {
    return Optional.empty();
  }

  AnnotatedElement element = optionalAnnotatedElement.get();
  Store store = context.getStore(NAMESPACE);

  Injector injector = store.get(element, Injector.class);
  if (injector == null) {
    injector = createInjector(context);
    store.put(element, injector);
  }

  return Optional.of(injector);
}
 
Example 6
Source File: DisabledOnWeekday.java    From JUnit-5-Quick-Start-Guide-and-Framework-Support with MIT License 6 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {

    // Search for the @DisabledWeekdays annotation from the TestExtensionContext
    Optional<AnnotatedElement> contextElement = context.getElement();
    AnnotatedElement annotatedElement = contextElement.orElse(null);

    if (annotatedElement == null) return null;

    DisabledWeekdays weekdayAnnotation = annotatedElement.getAnnotation(DisabledWeekdays.class);

    // Determine whether the test should be disabled
    boolean weekdayToday = IntStream.of(weekdayAnnotation.value())
            .anyMatch(day -> day == Calendar.getInstance().get(Calendar.DAY_OF_WEEK));

    // Return a ConditionEvaluationResult based on the outcome of the boolean weekdayToday
    return weekdayToday ?
            ConditionEvaluationResult.disabled("I spare you today.") :
            ConditionEvaluationResult.enabled("Don't spare you on other days though >:(");
}
 
Example 7
Source File: GuiceExtension.java    From bobcat with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all new {@link Module} declared in current context (returns empty set if modules where
 * already declared)
 */
private static Set<Class<? extends Module>> getNewModuleTypes(ExtensionContext context) {
  Optional<AnnotatedElement> optionalAnnotatedElement = context.getElement();
  if (!optionalAnnotatedElement.isPresent()) {
    return Collections.emptySet();
  }

  Set<Class<? extends Module>> moduleTypes = getModuleTypes(optionalAnnotatedElement.get());
  context.getParent()
      .map(GuiceExtension::getContextModuleTypes)
      .ifPresent(moduleTypes::removeAll);

  return moduleTypes;
}
 
Example 8
Source File: PrintUITestData.java    From JUnit-5-Quick-Start-Guide-and-Framework-Support with MIT License 5 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) throws Exception {
    Optional<AnnotatedElement> contextElement = context.getElement();
    AnnotatedElement annotatedElement = contextElement.orElse(null);

    if (annotatedElement != null) {
        UITest uiTest = annotatedElement.getAnnotation(UITest.class);
        LOG.info("Doing some setup for " + uiTest.value());
    }

}