Revise @DisabledIf support for JUnit Jupiter

- Extracted stand-alone DisabledIfCondition from the SpringExtension
  so that the condition is only evaluated when necessary.

- Simplified implementation of DisabledIfCondition.

- Overhauled and extended logging in DisabledIfCondition.

- DisabledIfCondition now throws an IllegalStateException if @DisabledIf
  is not present on the test element or if the expression does not
  evaluate to a String or Boolean.

- Each generated ConditionEvaluationResult now includes the actual
  expression in the default reason.

- @DisabledIf is now auto-configured to be evaluated by the
  DisabledIfCondition since it is now meta-annotated with
  @ExtendWith(DisabledIfCondition.class)

- Overhauled documentation for @DisabledIf and provided standard
  examples as well as an @DisabledOnMac annotation to demonstrate
  support for custom composed annotations.

Issue: SPR-14614
This commit is contained in:
Sam Brannen
2016-08-27 19:43:28 +02:00
parent c03f6c6d85
commit 19369094ac
5 changed files with 266 additions and 98 deletions

View File

@@ -18,27 +18,27 @@ package org.springframework.test.context.junit.jupiter;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;
/**
* Integration tests which demonstrate usage of {@link DisabledIf @DisabledIf}
* enabled by {@link SpringExtension} in a JUnit 5 (Jupiter) environment.
* Integration tests which verify support for {@link DisabledIf @DisabledIf}
* in conjunction with the {@link SpringExtension} in a JUnit 5 (Jupiter)
* environment.
*
* @author Tadaya Tsuyukubo
* @author Sam Brannen
* @since 5.0
* @see DisabledIf
* @see SpringExtension
*/
class DisabledIfTestCase {
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = Config.class)
@SpringJUnitConfig(Config.class)
@TestPropertySource(properties = "foo = true")
@Nested
class DisabledIfOnMethodTestCase {
@@ -73,6 +73,18 @@ class DisabledIfTestCase {
fail("This test must be disabled");
}
@Test
@DisabledIf("#{6 * 7 == 42}")
void disabledBySpelMathematicalComparison() {
fail("This test must be disabled");
}
@Test
@DisabledOnMac
void disabledBySpelOsCheckInCustomComposedAnnotation() {
assertFalse(System.getProperty("os.name").contains("Mac"), "This test must be disabled on Mac OS");
}
@Test
@DisabledIf("#{@booleanTrueBean}")
void disabledBySpelBooleanTrueBean() {
@@ -87,8 +99,7 @@ class DisabledIfTestCase {
}
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = Config.class)
@SpringJUnitConfig(Config.class)
@Nested
@DisabledIf("true")
class DisabledIfOnClassTestCase {
@@ -98,7 +109,8 @@ class DisabledIfTestCase {
fail("This test must be disabled");
}
// Even though method level condition is not disabling test, class level condition should take precedence
// Even though method level condition is not disabling test, class level condition
// should take precedence
@Test
@DisabledIf("false")
void bar() {
@@ -109,6 +121,7 @@ class DisabledIfTestCase {
@Configuration
static class Config {
@Bean
Boolean booleanTrueBean() {
return Boolean.TRUE;

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit.jupiter;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Demo <em>composed annotation</em> for {@link DisabledIf @DisabledIf} that
* disables a test class or test method if the current operating system is
* Mac OS.
*
* @author Sam Brannen
* @since 5.0
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@DisabledIf(expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}", reason = "Disabled on Mac OS")
public @interface DisabledOnMac {
}