Skip to content

Commit 2b261e6

Browse files
committed
Apply TestTypeExcludeFilter in regular applications
Add `ExcludeFilterApplicationContextInitializer` to register the `TestTypeExcludeFilter` for regular applications. Prior to this commit, the filter was only registered using the `ExcludeFilterContextCustomizerFactory` which meant that test components were filtered in tests but not when using `SpringApplication.from` with a test classpath. Fixes gh-35206
1 parent d913472 commit 2b261e6

File tree

5 files changed

+113
-3
lines changed

5 files changed

+113
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context.filter;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.context.ApplicationContextInitializer;
21+
import org.springframework.context.ConfigurableApplicationContext;
22+
23+
/**
24+
* {@link ApplicationContextInitializer} to register the {@link TestTypeExcludeFilter} for
25+
* when {@link SpringApplication#from} is being used with the test classpath.
26+
*
27+
* @author Phillip Webb
28+
*/
29+
class ExcludeFilterApplicationContextInitializer
30+
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
31+
32+
@Override
33+
public void initialize(ConfigurableApplicationContext applicationContext) {
34+
TestTypeExcludeFilter.registerWith(applicationContext.getBeanFactory());
35+
}
36+
37+
}

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/filter/ExcludeFilterContextCustomizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@ class ExcludeFilterContextCustomizer implements ContextCustomizer {
3232
@Override
3333
public void customizeContext(ConfigurableApplicationContext context,
3434
MergedContextConfiguration mergedContextConfiguration) {
35-
context.getBeanFactory().registerSingleton(TestTypeExcludeFilter.class.getName(), new TestTypeExcludeFilter());
35+
TestTypeExcludeFilter.registerWith(context.getBeanFactory());
3636
}
3737

3838
@Override

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/filter/TestTypeExcludeFilter.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020

21+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2122
import org.springframework.boot.context.TypeExcludeFilter;
2223
import org.springframework.boot.test.context.TestComponent;
2324
import org.springframework.core.type.classreading.MetadataReader;
@@ -32,13 +33,17 @@
3233
*/
3334
class TestTypeExcludeFilter extends TypeExcludeFilter {
3435

36+
private static final String BEAN_NAME = TestTypeExcludeFilter.class.getName();
37+
3538
private static final String[] CLASS_ANNOTATIONS = { "org.junit.runner.RunWith",
3639
"org.junit.jupiter.api.extension.ExtendWith", "org.junit.platform.commons.annotation.Testable",
3740
"org.testng.annotations.Test" };
3841

3942
private static final String[] METHOD_ANNOTATIONS = { "org.junit.Test",
4043
"org.junit.platform.commons.annotation.Testable", "org.testng.annotations.Test" };
4144

45+
private static final TestTypeExcludeFilter INSTANCE = new TestTypeExcludeFilter();
46+
4247
@Override
4348
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
4449
throws IOException {
@@ -91,4 +96,10 @@ private boolean isTestClass(MetadataReader metadataReader) {
9196
return false;
9297
}
9398

99+
static void registerWith(ConfigurableListableBeanFactory beanFactory) {
100+
if (!beanFactory.containsSingleton(BEAN_NAME)) {
101+
beanFactory.registerSingleton(BEAN_NAME, INSTANCE);
102+
}
103+
}
104+
94105
}

spring-boot-project/spring-boot-test/src/main/resources/META-INF/spring.factories

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener
1616
# Environment Post Processors
1717
org.springframework.boot.env.EnvironmentPostProcessor=\
1818
org.springframework.boot.test.web.SpringBootTestRandomPortEnvironmentPostProcessor
19+
20+
# Application Context Initializers
21+
org.springframework.context.ApplicationContextInitializer=\
22+
org.springframework.boot.test.context.filter.ExcludeFilterApplicationContextInitializer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context.filter;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.boot.WebApplicationType;
23+
import org.springframework.boot.context.TypeExcludeFilter;
24+
import org.springframework.boot.test.context.TestConfiguration;
25+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
26+
import org.springframework.context.annotation.ComponentScan;
27+
import org.springframework.context.annotation.ComponentScan.Filter;
28+
import org.springframework.context.annotation.FilterType;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests {@link ExcludeFilterApplicationContextInitializer}.
34+
*
35+
* @author Phillip Webb
36+
*/
37+
class ExcludeFilterApplicationContextInitializerTests {
38+
39+
@Test
40+
void testConfigurationIsExcluded() {
41+
SpringApplication application = new SpringApplication(TestApplication.class);
42+
application.setWebApplicationType(WebApplicationType.NONE);
43+
AssertableApplicationContext applicationContext = AssertableApplicationContext.get(() -> application.run());
44+
assertThat(applicationContext).hasSingleBean(TestApplication.class);
45+
assertThat(applicationContext).doesNotHaveBean(ExcludedTestConfiguration.class);
46+
}
47+
48+
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class) })
49+
static class TestApplication {
50+
51+
}
52+
53+
@TestConfiguration(proxyBeanMethods = false)
54+
static class ExcludedTestConfiguration {
55+
56+
}
57+
58+
}

0 commit comments

Comments
 (0)