Introduce @EnabledIf support for JUnit Jupiter

This commit picks up where SPR-14614 left off by introducing a new
@EnabledIf annotation to serve as a logical companion to @DisabledIf.

In addition, this commit extracts common logic from DisabledIfCondition
into a new AbstractExpressionEvaluatingCondition base class which the
new EnabledIfCondition also extends.

An @EnabledOnMac annotation is also included in the Javadoc as well as
in the test suite to demonstrate support for custom composed annotations.

Issue: SPR-14644
This commit is contained in:
Sam Brannen
2016-08-31 16:11:58 +02:00
parent 1a30252fc9
commit 634d1c03a3
9 changed files with 563 additions and 105 deletions

View File

@@ -34,7 +34,6 @@ import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -61,10 +60,8 @@ class DisabledIfConditionTestCase {
@Test
void missingDisabledIf() {
IllegalStateException exception = expectThrows(IllegalStateException.class,
() -> condition.evaluate(buildExtensionContext("missingDisabledIf")));
assertThat(exception.getMessage(), startsWith("@DisabledIf must be present"));
assertResult(condition.evaluate(buildExtensionContext("missingDisabledIf")), false,
endsWith("missingDisabledIf() is enabled since @DisabledIf is not present"));
}
@Test

View File

@@ -46,73 +46,73 @@ class DisabledIfTestCase {
@Test
@DisabledIf("true")
void disabledByStringTrue() {
void disabledIfWithStringTrue() {
fail("This test must be disabled");
}
@Test
@DisabledIf(" true ")
void disabledByStringTrueWithSurroundingWhitespace() {
void disabledIfWithStringTrueWithSurroundingWhitespace() {
fail("This test must be disabled");
}
@Test
@DisabledIf("TrUe")
void disabledByStringTrueIgnoreCase() {
void disabledIfWithStringTrueIgnoreCase() {
fail("This test must be disabled");
}
@Test
@DisabledIf("${foo}")
void disabledByPropertyPlaceholder() {
void disabledIfWithPropertyPlaceholder() {
fail("This test must be disabled");
}
@Test
@DisabledIf("\t${foo} ")
void disabledByPropertyPlaceholderWithSurroundingWhitespace() {
void disabledIfWithPropertyPlaceholderWithSurroundingWhitespace() {
fail("This test must be disabled");
}
@Test
@DisabledIf("#{T(java.lang.Boolean).TRUE}")
void disabledBySpelBoolean() {
@DisabledIf("#{T(Boolean).TRUE}")
void disabledIfWithSpelBoolean() {
fail("This test must be disabled");
}
@Test
@DisabledIf(" #{T(java.lang.Boolean).TRUE} ")
void disabledBySpelBooleanWithSurroundingWhitespace() {
@DisabledIf(" #{T(Boolean).TRUE} ")
void disabledIfWithSpelBooleanWithSurroundingWhitespace() {
fail("This test must be disabled");
}
@Test
@DisabledIf("#{'tr' + 'ue'}")
void disabledBySpelStringConcatenation() {
void disabledIfWithSpelStringConcatenation() {
fail("This test must be disabled");
}
@Test
@DisabledIf("#{6 * 7 == 42}")
void disabledBySpelMathematicalComparison() {
void disabledIfWithSpelArithmeticComparison() {
fail("This test must be disabled");
}
@Test
@DisabledOnMac
void disabledBySpelOsCheckInCustomComposedAnnotation() {
void disabledIfWithSpelOsCheckInCustomComposedAnnotation() {
assertFalse(System.getProperty("os.name").contains("Mac"), "This test must be disabled on Mac OS");
}
@Test
@DisabledIf("#{@booleanTrueBean}")
void disabledBySpelBooleanTrueBean() {
void disabledIfWithSpelBooleanTrueBean() {
fail("This test must be disabled");
}
@Test
@DisabledIf("#{@stringTrueBean}")
void disabledBySpelStringTrueBean() {
void disabledIfWithSpelStringTrueBean() {
fail("This test must be disabled");
}
@@ -128,12 +128,10 @@ class DisabledIfTestCase {
fail("This test must be disabled");
}
// Even though method level condition is not disabling test, class level condition
// should take precedence
@Test
@DisabledIf("false")
void bar() {
fail("This test must be disabled");
fail("This test must be disabled due to class-level condition");
}
}

View File

@@ -0,0 +1,154 @@
/*
* 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 org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Integration tests which verify support for {@link EnabledIf @EnabledIf}
* in conjunction with the {@link SpringExtension} in a JUnit 5 (Jupiter)
* environment.
*
* @author Tadaya Tsuyukubo
* @author Sam Brannen
* @since 5.0
* @see EnabledIfConditionTestCase
* @see EnabledIf
* @see SpringExtension
*/
class EnabledIfTestCase {
@SpringJUnitConfig(Config.class)
@TestPropertySource(properties = "foo = false")
@Nested
class EnabledIfOnMethodTestCase {
@Test
@EnabledIf("false")
void enabledIfWithStringFalse() {
fail("This test must be disabled");
}
@Test
@EnabledIf(" false ")
void enabledIfWithStringFalseWithSurroundingWhitespace() {
fail("This test must be disabled");
}
@Test
@EnabledIf("FaLsE")
void enabledIfWithStringFalseIgnoreCase() {
fail("This test must be disabled");
}
@Test
@EnabledIf("${foo}")
void enabledIfWithPropertyPlaceholder() {
fail("This test must be disabled");
}
@Test
@EnabledIf("\t${foo} ")
void enabledIfWithPropertyPlaceholderWithSurroundingWhitespace() {
fail("This test must be disabled");
}
@Test
@EnabledIf("#{T(Boolean).FALSE}")
void enabledIfWithSpelBoolean() {
fail("This test must be disabled");
}
@Test
@EnabledIf(" #{T(Boolean).FALSE} ")
void enabledIfWithSpelBooleanWithSurroundingWhitespace() {
fail("This test must be disabled");
}
@Test
@EnabledIf("#{'fal' + 'se'}")
void enabledIfWithSpelStringConcatenation() {
fail("This test must be disabled");
}
@Test
@EnabledIf("#{1 + 2 == 4}")
void enabledIfWithSpelArithmeticComparison() {
fail("This test must be disabled");
}
@Test
@EnabledOnMac
void enabledIfWithSpelOsCheckInCustomComposedAnnotation() {
String os = System.getProperty("os.name").toLowerCase();
assertTrue(os.contains("mac"), "This test must be enabled on Mac OS");
assertFalse(os.contains("win"), "This test must be disabled on Windows");
}
@Test
@EnabledIf("#{@booleanFalseBean}")
void enabledIfWithSpelBooleanFalseBean() {
fail("This test must be disabled");
}
@Test
@EnabledIf("#{@stringFalseBean}")
void enabledIfWithSpelStringFalseBean() {
fail("This test must be disabled");
}
}
@SpringJUnitConfig(Config.class)
@Nested
@EnabledIf("false")
class EnabledIfOnClassTestCase {
@Test
void foo() {
fail("This test must be disabled");
}
@Test
@EnabledIf("true")
void bar() {
fail("This test must be disabled due to class-level condition");
}
}
@Configuration
static class Config {
@Bean
Boolean booleanFalseBean() {
return Boolean.FALSE;
}
@Bean
String stringFalseBean() {
return "false";
}
}
}

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 EnabledIf @EnabledIf} that
* enables 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
@EnabledIf(expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}", reason = "Enabled on Mac OS")
public @interface EnabledOnMac {
}