Skip to content

Commit 524c0e1

Browse files
committed
Merge branch '2.7.x' into main
2 parents 6f8ce3d + b80047c commit 524c0e1

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ include::code:MyBatchConfiguration[]
782782

783783
NOTE: Depending on the complexity of your application, you may either have a single `@Configuration` class for your customizations or one class per domain area.
784784
The latter approach lets you enable it in one of your tests, if necessary, with the `@Import` annotation.
785+
See <<howto#howto.testing.slice-tests,this how-to section>> for more details on when you might want to enable specific `@Configuration` classes for slice tests.
785786

786787
Test slices exclude `@Configuration` classes from scanning.
787788
For example, for a `@WebMvcTest`, the following configuration will not include the given `WebMvcConfigurer` bean in the application context loaded by the test slice:

spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto/testing.adoc

+33
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,36 @@ This can be done with a static `@DynamicPropertySource` method that allows addin
3636
include::code:dynamicproperties/MyIntegrationTests[]
3737

3838
The above configuration allows Neo4j-related beans in the application to communicate with Neo4j running inside the Testcontainers-managed Docker container.
39+
40+
41+
42+
[[howto.testing.slice-tests]]
43+
=== Structure `@Configuration` classes for inclusion in slice tests
44+
Slice tests work by restricting Spring Framework's component scanning to a limited set of components based on their type.
45+
For any beans that are not created via component scanning, for example, beans that are created using the `@Bean` annotation, slice tests will not be able to include/exclude them from the application context.
46+
Consider this example:
47+
48+
[source,java,indent=0,subs="verbatim"]
49+
----
50+
include::{docs-java}/howto/testing/slicetests/MyConfiguration.java[]
51+
----
52+
53+
For a `@WebMvcTest` for an application with the above `@Configuration` class, you might expect to have the `SecurityFilterChain` bean in the application context so that you can test if your controller endpoints are secured properly.
54+
However, `MyConfiguration` is not picked up by @WebMvcTest's component scanning filter because it doesn't match any of the types specified by the filter.
55+
You can include the configuration explicitly by annotating the test class with `@Import(MySecurityConfiguration.class)`.
56+
This will load all the beans in `MyConfiguration` including the `BasicDataSource` bean which isn't required when testing the web tier.
57+
Splitting the configuration class into two will enable importing just the security configuration.
58+
59+
[source,java,indent=0,subs="verbatim"]
60+
----
61+
include::{docs-java}/howto/testing/slicetests/MySecurityConfiguration.java[]
62+
----
63+
64+
[source,java,indent=0,subs="verbatim"]
65+
----
66+
include::{docs-java}/howto/testing/slicetests/MyDatasourceConfiguration.java[]
67+
----
68+
69+
Having a single configuration class can be inefficient when beans of a certain domain needed to be included in slice tests.
70+
Instead, structuring the application's configuration as multiple granular classes with beans for a specific domain can enable importing them only for specific slice tests.
71+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2022 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.docs.howto.testing.slicetests;
18+
19+
import org.apache.commons.dbcp2.BasicDataSource;
20+
21+
import org.springframework.boot.context.properties.ConfigurationProperties;
22+
import org.springframework.boot.jdbc.DataSourceBuilder;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
26+
import org.springframework.security.web.SecurityFilterChain;
27+
28+
@Configuration(proxyBeanMethods = false)
29+
public class MyConfiguration {
30+
31+
@Bean
32+
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
33+
http.authorizeRequests().anyRequest().authenticated();
34+
return http.build();
35+
}
36+
37+
@Bean
38+
@ConfigurationProperties("app.datasource.second")
39+
public BasicDataSource secondDataSource() {
40+
return DataSourceBuilder.create().type(BasicDataSource.class).build();
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2022 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.docs.howto.testing.slicetests;
18+
19+
import org.apache.commons.dbcp2.BasicDataSource;
20+
21+
import org.springframework.boot.context.properties.ConfigurationProperties;
22+
import org.springframework.boot.jdbc.DataSourceBuilder;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
26+
@Configuration(proxyBeanMethods = false)
27+
class MyDatasourceConfiguration {
28+
29+
@Bean
30+
@ConfigurationProperties("app.datasource.second")
31+
public BasicDataSource secondDataSource() {
32+
return DataSourceBuilder.create().type(BasicDataSource.class).build();
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2022 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.docs.howto.testing.slicetests;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
22+
import org.springframework.security.web.SecurityFilterChain;
23+
24+
@Configuration(proxyBeanMethods = false)
25+
class MySecurityConfiguration {
26+
27+
@Bean
28+
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
29+
http.authorizeRequests().anyRequest().authenticated();
30+
return http.build();
31+
}
32+
33+
}

0 commit comments

Comments
 (0)