forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Honor class-level @DirtiesContext if test class is disabled via SpEL
Prior to this commit, if a test class annotated with @DirtiesContext and @EnabledIf/@DisabledIf with `loadContext = true` was disabled due to the evaluated SpEL expression, the ApplicationContext would not be marked as dirty and closed. The reason is that @EnabledIf/@DisabledIf are implemented via JUnit Jupiter's ExecutionCondition extension API which results in the entire test class (as well as any associated extension callbacks) being skipped if the condition evaluates to `disabled`. This effectively prevents any of Spring's TestExecutionListener APIs from being invoked. Consequently, the DirtiesContextTestExecutionListener does not get a chance to honor the class-level @DirtiesContext declaration. This commit fixes this by implementing part of the logic of DirtiesContextTestExecutionListener in AbstractExpressionEvaluatingCondition (i.e., the base class for @EnabledIf/@DisabledIf support). Specifically, if the test class for an eagerly loaded ApplicationContext is disabled, AbstractExpressionEvaluatingCondition will now mark the test ApplicationContext as dirty if the test class is annotated with @DirtiesContext. Closes spring-projectsgh-26694
- Loading branch information
Showing
5 changed files
with
241 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...java/org/springframework/test/context/junit/jupiter/DisabledIfAndDirtiesContextTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2002-2021 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 | ||
* | ||
* https://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.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.testkit.engine.EngineTestKit; | ||
|
||
import org.springframework.beans.factory.DisposableBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; | ||
|
||
/** | ||
* Integration tests which verify support for {@link DisabledIf @DisabledIf} in | ||
* conjunction with {@link DirtiesContext @DirtiesContext} and the | ||
* {@link SpringExtension} in a JUnit Jupiter environment. | ||
* | ||
* @author Sam Brannen | ||
* @since 5.2.14 | ||
* @see EnabledIfAndDirtiesContextTests | ||
*/ | ||
class DisabledIfAndDirtiesContextTests { | ||
|
||
private static AtomicBoolean contextClosed = new AtomicBoolean(); | ||
|
||
|
||
@BeforeEach | ||
void reset() { | ||
contextClosed.set(false); | ||
} | ||
|
||
@Test | ||
void contextShouldBeClosedForEnabledTestClass() { | ||
assertThat(contextClosed).as("context closed").isFalse(); | ||
EngineTestKit.engine("junit-jupiter").selectors( | ||
selectClass(EnabledAndDirtiesContextTestCase.class))// | ||
.execute()// | ||
.testEvents()// | ||
.assertStatistics(stats -> stats.started(1).succeeded(1).failed(0)); | ||
assertThat(contextClosed).as("context closed").isTrue(); | ||
} | ||
|
||
@Test | ||
void contextShouldBeClosedForDisabledTestClass() { | ||
assertThat(contextClosed).as("context closed").isFalse(); | ||
EngineTestKit.engine("junit-jupiter").selectors( | ||
selectClass(DisabledAndDirtiesContextTestCase.class))// | ||
.execute()// | ||
.testEvents()// | ||
.assertStatistics(stats -> stats.started(0).succeeded(0).failed(0)); | ||
assertThat(contextClosed).as("context closed").isTrue(); | ||
} | ||
|
||
|
||
@SpringJUnitConfig(Config.class) | ||
@DisabledIf(expression = "false", loadContext = true) | ||
@DirtiesContext | ||
static class EnabledAndDirtiesContextTestCase { | ||
|
||
@Test | ||
void test() { | ||
/* no-op */ | ||
} | ||
} | ||
|
||
@SpringJUnitConfig(Config.class) | ||
@DisabledIf(expression = "true", loadContext = true) | ||
@DirtiesContext | ||
static class DisabledAndDirtiesContextTestCase { | ||
|
||
@Test | ||
void test() { | ||
fail("This test must be disabled"); | ||
} | ||
} | ||
|
||
@Configuration | ||
static class Config { | ||
|
||
@Bean | ||
DisposableBean disposableBean() { | ||
return () -> contextClosed.set(true); | ||
} | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
.../java/org/springframework/test/context/junit/jupiter/EnabledIfAndDirtiesContextTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2002-2021 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 | ||
* | ||
* https://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.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.testkit.engine.EngineTestKit; | ||
|
||
import org.springframework.beans.factory.DisposableBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; | ||
|
||
/** | ||
* Integration tests which verify support for {@link EnabledIf @EnabledIf} in | ||
* conjunction with {@link DirtiesContext @DirtiesContext} and the | ||
* {@link SpringExtension} in a JUnit Jupiter environment. | ||
* | ||
* @author Sam Brannen | ||
* @since 5.2.14 | ||
* @see DisabledIfAndDirtiesContextTests | ||
*/ | ||
class EnabledIfAndDirtiesContextTests { | ||
|
||
private static AtomicBoolean contextClosed = new AtomicBoolean(); | ||
|
||
|
||
@BeforeEach | ||
void reset() { | ||
contextClosed.set(false); | ||
} | ||
|
||
@Test | ||
void contextShouldBeClosedForEnabledTestClass() { | ||
assertThat(contextClosed).as("context closed").isFalse(); | ||
EngineTestKit.engine("junit-jupiter").selectors( | ||
selectClass(EnabledAndDirtiesContextTestCase.class))// | ||
.execute()// | ||
.testEvents()// | ||
.assertStatistics(stats -> stats.started(1).succeeded(1).failed(0)); | ||
assertThat(contextClosed).as("context closed").isTrue(); | ||
} | ||
|
||
@Test | ||
void contextShouldBeClosedForDisabledTestClass() { | ||
assertThat(contextClosed).as("context closed").isFalse(); | ||
EngineTestKit.engine("junit-jupiter").selectors( | ||
selectClass(DisabledAndDirtiesContextTestCase.class))// | ||
.execute()// | ||
.testEvents()// | ||
.assertStatistics(stats -> stats.started(0).succeeded(0).failed(0)); | ||
assertThat(contextClosed).as("context closed").isTrue(); | ||
} | ||
|
||
|
||
@SpringJUnitConfig(Config.class) | ||
@EnabledIf(expression = "true", loadContext = true) | ||
@DirtiesContext | ||
static class EnabledAndDirtiesContextTestCase { | ||
|
||
@Test | ||
void test() { | ||
/* no-op */ | ||
} | ||
} | ||
|
||
@SpringJUnitConfig(Config.class) | ||
@EnabledIf(expression = "false", loadContext = true) | ||
@DirtiesContext | ||
static class DisabledAndDirtiesContextTestCase { | ||
|
||
@Test | ||
void test() { | ||
fail("This test must be disabled"); | ||
} | ||
} | ||
|
||
@Configuration | ||
static class Config { | ||
|
||
@Bean | ||
DisposableBean disposableBean() { | ||
return () -> contextClosed.set(true); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters