Support @EnabledIf & @DisabledIf w/o loading ApplicationContext
Prior to this commit, when using @EnabledIf or @DisabledIf in Spring's JUnit Jupiter support, the test's ApplicationContext was always eagerly loaded, even if the ApplicationContext would never be used (i.e., the test was disabled). This behavior can lead to undesirable side effects -- for example, attempting to load an application context that requires services only available on the CI server when the test is not actually running on the CI server. This commit addresses this issue by introducing new boolean `loadContext` flags in @EnabledIf and @DisabledIf. By default these flags are set to false which means that the user's test application context will not be loaded to evaluate the expression. On the contrary, a dummy application context will be loaded instead, and expressions will be evaluated against that dummy context. Consequently, if the user wishes to interact with properties from the Spring Environment or with beans from the test application context, the `loadContext` must be explicitly set to true. In addition, expressions which evaluate to a String must now evaluate to exactly "true" or "false" (ignoring case and surrounding whitespace). Issue: SPR-14649
This commit is contained in:
@@ -76,11 +76,26 @@ class DisabledIfConditionTestCase {
|
||||
|
||||
@Test
|
||||
void invalidExpressionEvaluationType() {
|
||||
String methodName = "nonBooleanOrStringExpression";
|
||||
IllegalStateException exception = expectThrows(IllegalStateException.class,
|
||||
() -> condition.evaluate(buildExtensionContext("nonBooleanOrStringExpression")));
|
||||
() -> condition.evaluate(buildExtensionContext(methodName)));
|
||||
|
||||
Method method = ReflectionUtils.findMethod(getClass(), methodName);
|
||||
|
||||
assertThat(exception.getMessage(),
|
||||
is(equalTo("@DisabledIf(\"#{6 * 7}\") must evaluate to a String or a Boolean, not java.lang.Integer")));
|
||||
is(equalTo("@DisabledIf(\"#{6 * 7}\") on " + method + " must evaluate to a String or a Boolean, not java.lang.Integer")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsupportedStringEvaluationValue() {
|
||||
String methodName = "stringExpressionThatIsNeitherTrueNorFalse";
|
||||
IllegalStateException exception = expectThrows(IllegalStateException.class,
|
||||
() -> condition.evaluate(buildExtensionContext(methodName)));
|
||||
|
||||
Method method = ReflectionUtils.findMethod(getClass(), methodName);
|
||||
|
||||
assertThat(exception.getMessage(),
|
||||
is(equalTo("@DisabledIf(\"#{'enigma'}\") on " + method + " must evaluate to \"true\" or \"false\", not \"enigma\"")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -151,6 +166,10 @@ class DisabledIfConditionTestCase {
|
||||
private void nonBooleanOrStringExpression() {
|
||||
}
|
||||
|
||||
@DisabledIf("#{'enigma'}")
|
||||
private void stringExpressionThatIsNeitherTrueNorFalse() {
|
||||
}
|
||||
|
||||
@DisabledIf(expression = "#{6 * 7 == 42}", reason = "Because... 42!")
|
||||
private void customReason() {
|
||||
}
|
||||
|
||||
@@ -23,7 +23,8 @@ 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.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Integration tests which verify support for {@link DisabledIf @DisabledIf}
|
||||
@@ -63,13 +64,19 @@ class DisabledIfTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledIf("${foo}")
|
||||
@DisabledIf("${__EnigmaPropertyShouldNotExist__:true}")
|
||||
void disabledIfWithPropertyPlaceholderForNonexistentPropertyWithDefaultValue() {
|
||||
fail("This test must be disabled");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledIf(expression = "${foo}", loadContext = true)
|
||||
void disabledIfWithPropertyPlaceholder() {
|
||||
fail("This test must be disabled");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledIf("\t${foo} ")
|
||||
@DisabledIf(expression = "\t${foo} ", loadContext = true)
|
||||
void disabledIfWithPropertyPlaceholderWithSurroundingWhitespace() {
|
||||
fail("This test must be disabled");
|
||||
}
|
||||
@@ -105,13 +112,13 @@ class DisabledIfTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledIf("#{@booleanTrueBean}")
|
||||
@DisabledIf(expression = "#{@booleanTrueBean}", loadContext = true)
|
||||
void disabledIfWithSpelBooleanTrueBean() {
|
||||
fail("This test must be disabled");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledIf("#{@stringTrueBean}")
|
||||
@DisabledIf(expression = "#{@stringTrueBean}", loadContext = true)
|
||||
void disabledIfWithSpelStringTrueBean() {
|
||||
fail("This test must be disabled");
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ 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;
|
||||
@@ -64,13 +65,19 @@ class EnabledIfTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledIf("${foo}")
|
||||
@EnabledIf("${__EnigmaPropertyShouldNotExist__:false}")
|
||||
void enabledIfWithPropertyPlaceholderForNonexistentPropertyWithDefaultValue() {
|
||||
fail("This test must be disabled");
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledIf(expression = "${foo}", loadContext = true)
|
||||
void enabledIfWithPropertyPlaceholder() {
|
||||
fail("This test must be disabled");
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledIf("\t${foo} ")
|
||||
@EnabledIf(expression = "\t${foo} ", loadContext = true)
|
||||
void enabledIfWithPropertyPlaceholderWithSurroundingWhitespace() {
|
||||
fail("This test must be disabled");
|
||||
}
|
||||
@@ -108,13 +115,13 @@ class EnabledIfTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledIf("#{@booleanFalseBean}")
|
||||
@EnabledIf(expression = "#{@booleanFalseBean}", loadContext = true)
|
||||
void enabledIfWithSpelBooleanFalseBean() {
|
||||
fail("This test must be disabled");
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledIf("#{@stringFalseBean}")
|
||||
@EnabledIf(expression = "#{@stringFalseBean}", loadContext = true)
|
||||
void enabledIfWithSpelStringFalseBean() {
|
||||
fail("This test must be disabled");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user