-
Notifications
You must be signed in to change notification settings - Fork 38.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clear ApplicationContext cache after a test class completes if necessary
The ApplicationContext has a resource cache that it uses while the context is being refreshed. Once the refresh phase completes, the cache is cleared as its content is no longer in use. If beans are lazily initialized, there is a case where the cache might be filled again as processing is deferred past the refresh phase. For those cases, the cache is not cleared. This can be a problem when running in this mode with a large set of test configurations as the TCF caches the related contexts. This commit includes an additional TestExecutionListener to the TCF that clears the resource cache if necessary once a test class completes. It is ordered so that it runs after `@DirtiesContext` support as marking the context as dirty will remove it from the cache and make that extra step unnecessary. Closes gh-30954
- Loading branch information
Showing
6 changed files
with
209 additions
and
0 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
55 changes: 55 additions & 0 deletions
55
.../main/java/org/springframework/test/context/support/CommonCacheTestExecutionListener.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,55 @@ | ||
/* | ||
* Copyright 2002-2024 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.support; | ||
|
||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.support.AbstractApplicationContext; | ||
import org.springframework.test.context.TestContext; | ||
|
||
/** | ||
* {@code TestExecutionListener} which makes sure that caches are cleared once | ||
* they are no longer required. Clears the resource cache of the | ||
* {@link ApplicationContext} as it is only required during the beans | ||
* initialization phase. Runs after {@link DirtiesContextTestExecutionListener} | ||
* as dirtying the context will remove it from the cache and make this | ||
* unnecessary. | ||
* | ||
* @author Stephane Nicoll | ||
* @since 6.2 | ||
*/ | ||
public class CommonCacheTestExecutionListener extends AbstractTestExecutionListener { | ||
|
||
/** | ||
* Returns {@code 3005}. | ||
*/ | ||
@Override | ||
public final int getOrder() { | ||
return 3005; | ||
} | ||
|
||
|
||
@Override | ||
public void afterTestClass(TestContext testContext) throws Exception { | ||
if (testContext.hasApplicationContext()) { | ||
ApplicationContext applicationContext = testContext.getApplicationContext(); | ||
if (applicationContext instanceof AbstractApplicationContext ctx) { | ||
ctx.clearResourceCaches(); | ||
} | ||
} | ||
} | ||
|
||
} |
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
90 changes: 90 additions & 0 deletions
90
...src/test/java/org/springframework/test/context/cache/SpringExtensionCommonCacheTests.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,90 @@ | ||
/* | ||
* Copyright 2002-2024 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.cache; | ||
|
||
import org.junit.jupiter.api.ClassOrderer; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Order; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestClassOrder; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.context.support.AbstractApplicationContext; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.core.type.classreading.MetadataReader; | ||
import org.springframework.test.context.cache.SpringExtensionCommonCacheTests.TestConfiguration; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests that verify that common caches are cleared at the end of a test | ||
* class. Regular callback cannot be used to validate this as they run | ||
* before the listener, so we need two test classes that are ordered to | ||
* validate the result. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
@SpringJUnitConfig(classes = TestConfiguration.class) | ||
@TestClassOrder(ClassOrderer.OrderAnnotation.class) | ||
class SpringExtensionCommonCacheTests { | ||
|
||
@Autowired | ||
AbstractApplicationContext applicationContext; | ||
|
||
@Nested | ||
@Order(1) | ||
class FirstTests { | ||
|
||
@Test | ||
void lazyInitBeans() { | ||
applicationContext.getBean(String.class); | ||
assertThat(applicationContext.getResourceCache(MetadataReader.class)).isNotEmpty(); | ||
} | ||
|
||
} | ||
|
||
@Nested | ||
@Order(2) | ||
class SecondTests { | ||
|
||
@Test | ||
void validateCommonCacheIsCleared() { | ||
assertThat(applicationContext.getResourceCache(MetadataReader.class)).isEmpty(); | ||
} | ||
|
||
} | ||
|
||
|
||
@Configuration | ||
static class TestConfiguration { | ||
|
||
@Bean | ||
@Lazy | ||
String dummyBean(ResourceLoader resourceLoader) { | ||
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(true); | ||
scanner.setResourceLoader(resourceLoader); | ||
scanner.findCandidateComponents(TestConfiguration.class.getPackageName()); | ||
return "Dummy"; | ||
} | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
.../java/org/springframework/test/context/support/CommonCacheTestExecutionListenerTests.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,57 @@ | ||
/* | ||
* Copyright 2002-2024 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.support; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.context.support.AbstractApplicationContext; | ||
import org.springframework.test.context.TestContext; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
|
||
/** | ||
* Tests for {@link CommonCacheTestExecutionListener}. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
class CommonCacheTestExecutionListenerTests { | ||
|
||
private final CommonCacheTestExecutionListener listener = new CommonCacheTestExecutionListener(); | ||
|
||
@Test | ||
void afterTestClassWhenContextIsAvailable() throws Exception { | ||
AbstractApplicationContext applicationContext = mock(AbstractApplicationContext.class); | ||
TestContext testContext = mock(TestContext.class); | ||
given(testContext.hasApplicationContext()).willReturn(true); | ||
given(testContext.getApplicationContext()).willReturn(applicationContext); | ||
listener.afterTestClass(testContext); | ||
verify(applicationContext).clearResourceCaches(); | ||
} | ||
|
||
@Test | ||
void afterTestClassCWhenContextIsNotAvailable() throws Exception { | ||
TestContext testContext = mock(TestContext.class); | ||
given(testContext.hasApplicationContext()).willReturn(false); | ||
listener.afterTestClass(testContext); | ||
verify(testContext).hasApplicationContext(); | ||
verifyNoMoreInteractions(testContext); | ||
} | ||
|
||
} |