Skip to content

Commit 948902f

Browse files
dreis2211snicoll
authored andcommitted
Avoid unnecessary usage of ReflectionTestUtils
Closes gh-15482
1 parent d004ee9 commit 948902f

File tree

11 files changed

+44
-83
lines changed

11 files changed

+44
-83
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323
import org.springframework.context.annotation.Bean;
2424
import org.springframework.context.annotation.Configuration;
2525
import org.springframework.context.annotation.Primary;
26-
import org.springframework.ldap.core.ContextSource;
2726
import org.springframework.ldap.core.LdapTemplate;
2827
import org.springframework.ldap.core.support.LdapContextSource;
2928
import org.springframework.ldap.pool2.factory.PoolConfig;
3029
import org.springframework.ldap.pool2.factory.PooledContextSource;
31-
import org.springframework.test.util.ReflectionTestUtils;
3230

3331
import static org.assertj.core.api.Assertions.assertThat;
3432

@@ -58,7 +56,7 @@ public void contextSourceWithDefaultUrl() {
5856
public void contextSourceWithSingleUrl() {
5957
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:123")
6058
.run((context) -> {
61-
ContextSource contextSource = context
59+
LdapContextSource contextSource = context
6260
.getBean(LdapContextSource.class);
6361
String[] urls = getUrls(contextSource);
6462
assertThat(urls).containsExactly("ldap://localhost:123");
@@ -71,7 +69,7 @@ public void contextSourceWithSeveralUrls() {
7169
.withPropertyValues(
7270
"spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123")
7371
.run((context) -> {
74-
ContextSource contextSource = context
72+
LdapContextSource contextSource = context
7573
.getBean(LdapContextSource.class);
7674
LdapProperties ldapProperties = context.getBean(LdapProperties.class);
7775
String[] urls = getUrls(contextSource);
@@ -120,8 +118,8 @@ public void contextSourceWithUserProvidedPooledContextSource() {
120118
});
121119
}
122120

123-
private String[] getUrls(ContextSource contextSource) {
124-
return (String[]) ReflectionTestUtils.getField(contextSource, "urls");
121+
private String[] getUrls(LdapContextSource contextSource) {
122+
return contextSource.getUrls();
125123
}
126124

127125
@Configuration

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactoryTests.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
import com.mongodb.MongoClient;
2222
import com.mongodb.MongoCredential;
2323
import com.mongodb.ServerAddress;
24-
import com.mongodb.connection.Cluster;
25-
import com.mongodb.connection.ClusterSettings;
2624
import org.junit.Test;
2725

2826
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2927
import org.springframework.context.annotation.Configuration;
3028
import org.springframework.core.env.Environment;
3129
import org.springframework.mock.env.MockEnvironment;
32-
import org.springframework.test.util.ReflectionTestUtils;
3330

3431
import static org.assertj.core.api.Assertions.assertThat;
3532

@@ -50,7 +47,7 @@ public void portCanBeCustomized() {
5047
MongoProperties properties = new MongoProperties();
5148
properties.setPort(12345);
5249
MongoClient client = createMongoClient(properties);
53-
List<ServerAddress> allAddresses = extractServerAddresses(client);
50+
List<ServerAddress> allAddresses = client.getAllAddress();
5451
assertThat(allAddresses).hasSize(1);
5552
assertServerAddress(allAddresses.get(0), "localhost", 12345);
5653
}
@@ -60,7 +57,7 @@ public void hostCanBeCustomized() {
6057
MongoProperties properties = new MongoProperties();
6158
properties.setHost("mongo.example.com");
6259
MongoClient client = createMongoClient(properties);
63-
List<ServerAddress> allAddresses = extractServerAddresses(client);
60+
List<ServerAddress> allAddresses = client.getAllAddress();
6461
assertThat(allAddresses).hasSize(1);
6562
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
6663
}
@@ -103,7 +100,7 @@ public void uriCanBeCustomized() {
103100
properties.setUri("mongodb://user:[email protected]:12345,"
104101
+ "mongo2.example.com:23456/test");
105102
MongoClient client = createMongoClient(properties);
106-
List<ServerAddress> allAddresses = extractServerAddresses(client);
103+
List<ServerAddress> allAddresses = client.getAllAddress();
107104
assertThat(allAddresses).hasSize(2);
108105
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
109106
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
@@ -118,7 +115,7 @@ public void uriIsIgnoredInEmbeddedMode() {
118115
properties.setUri("mongodb://mongo.example.com:1234/mydb");
119116
this.environment.setProperty("local.mongo.port", "4000");
120117
MongoClient client = createMongoClient(properties, this.environment);
121-
List<ServerAddress> allAddresses = extractServerAddresses(client);
118+
List<ServerAddress> allAddresses = client.getAllAddress();
122119
assertThat(allAddresses).hasSize(1);
123120
assertServerAddress(allAddresses.get(0), "localhost", 4000);
124121
}
@@ -132,14 +129,6 @@ private MongoClient createMongoClient(MongoProperties properties,
132129
return new MongoClientFactory(properties, environment).createMongoClient(null);
133130
}
134131

135-
private List<ServerAddress> extractServerAddresses(MongoClient client) {
136-
Cluster cluster = (Cluster) ReflectionTestUtils
137-
.getField(ReflectionTestUtils.getField(client, "delegate"), "cluster");
138-
ClusterSettings clusterSettings = (ClusterSettings) ReflectionTestUtils
139-
.getField(cluster, "settings");
140-
return clusterSettings.getHosts();
141-
}
142-
143132
private void assertServerAddress(ServerAddress serverAddress, String expectedHost,
144133
int expectedPort) {
145134
assertThat(serverAddress.getHost()).isEqualTo(expectedHost);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
import com.mongodb.MongoClient;
2222
import com.mongodb.MongoClientOptions;
2323
import com.mongodb.ServerAddress;
24-
import com.mongodb.connection.Cluster;
25-
import com.mongodb.connection.ClusterSettings;
2624
import org.junit.Test;
2725

2826
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2927
import org.springframework.boot.test.util.TestPropertyValues;
3028
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3129
import org.springframework.context.annotation.Configuration;
32-
import org.springframework.test.util.ReflectionTestUtils;
3330

3431
import static org.assertj.core.api.Assertions.assertThat;
3532

@@ -120,7 +117,7 @@ public void uriOverridesHostAndPort() {
120117
properties.setUri("mongodb://mongo1.example.com:12345");
121118
MongoClient client = new MongoClientFactory(properties, null)
122119
.createMongoClient(null);
123-
List<ServerAddress> allAddresses = extractServerAddresses(client);
120+
List<ServerAddress> allAddresses = client.getAllAddress();
124121
assertThat(allAddresses).hasSize(1);
125122
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
126123
}
@@ -132,7 +129,7 @@ public void onlyHostAndPortSetShouldUseThat() {
132129
properties.setPort(27017);
133130
MongoClient client = new MongoClientFactory(properties, null)
134131
.createMongoClient(null);
135-
List<ServerAddress> allAddresses = extractServerAddresses(client);
132+
List<ServerAddress> allAddresses = client.getAllAddress();
136133
assertThat(allAddresses).hasSize(1);
137134
assertServerAddress(allAddresses.get(0), "localhost", 27017);
138135
}
@@ -143,7 +140,7 @@ public void onlyUriSetShouldUseThat() {
143140
properties.setUri("mongodb://mongo1.example.com:12345");
144141
MongoClient client = new MongoClientFactory(properties, null)
145142
.createMongoClient(null);
146-
List<ServerAddress> allAddresses = extractServerAddresses(client);
143+
List<ServerAddress> allAddresses = client.getAllAddress();
147144
assertThat(allAddresses).hasSize(1);
148145
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
149146
}
@@ -153,19 +150,11 @@ public void noCustomAddressAndNoUriUsesDefaultUri() {
153150
MongoProperties properties = new MongoProperties();
154151
MongoClient client = new MongoClientFactory(properties, null)
155152
.createMongoClient(null);
156-
List<ServerAddress> allAddresses = extractServerAddresses(client);
153+
List<ServerAddress> allAddresses = client.getAllAddress();
157154
assertThat(allAddresses).hasSize(1);
158155
assertServerAddress(allAddresses.get(0), "localhost", 27017);
159156
}
160157

161-
private List<ServerAddress> extractServerAddresses(MongoClient client) {
162-
Cluster cluster = (Cluster) ReflectionTestUtils
163-
.getField(ReflectionTestUtils.getField(client, "delegate"), "cluster");
164-
ClusterSettings clusterSettings = (ClusterSettings) ReflectionTestUtils
165-
.getField(cluster, "settings");
166-
return clusterSettings.getHosts();
167-
}
168-
169158
private void assertServerAddress(ServerAddress serverAddress, String expectedHost,
170159
int expectedPort) {
171160
assertThat(serverAddress.getHost()).isEqualTo(expectedHost);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/servlet/OAuth2WebSecurityConfigurationTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,12 @@ public void authorizedClientRepositoryBeanIsConditionalOnMissingBean() {
165165
});
166166
}
167167

168-
@SuppressWarnings("unchecked")
169168
private List<Filter> getFilters(AssertableApplicationContext context,
170169
Class<? extends Filter> filter) {
171170
FilterChainProxy filterChain = (FilterChainProxy) context
172171
.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN);
173172
List<SecurityFilterChain> filterChains = filterChain.getFilterChains();
174-
List<Filter> filters = (List<Filter>) ReflectionTestUtils
175-
.getField(filterChains.get(0), "filters");
173+
List<Filter> filters = filterChains.get(0).getFilters();
176174
return filters.stream().filter(filter::isInstance).collect(Collectors.toList());
177175
}
178176

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/reactive/ReactiveOAuth2ResourceServerAutoConfigurationTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import java.io.IOException;
1919
import java.util.Collections;
2020
import java.util.HashMap;
21-
import java.util.List;
2221
import java.util.Map;
22+
import java.util.stream.Stream;
2323

2424
import okhttp3.mockwebserver.MockResponse;
2525
import okhttp3.mockwebserver.MockWebServer;
@@ -175,14 +175,12 @@ public void autoConfigurationWhenSecurityWebFilterChainConfigPresentShouldNotAdd
175175
});
176176
}
177177

178-
@SuppressWarnings("unchecked")
179178
private void assertFilterConfiguredWithJwtAuthenticationManager(
180179
AssertableReactiveWebApplicationContext context) {
181180
MatcherSecurityWebFilterChain filterChain = (MatcherSecurityWebFilterChain) context
182181
.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN);
183-
List<WebFilter> filters = (List<WebFilter>) ReflectionTestUtils
184-
.getField(filterChain, "filters");
185-
AuthenticationWebFilter webFilter = (AuthenticationWebFilter) filters.stream()
182+
Stream<WebFilter> filters = filterChain.getWebFilters().toStream();
183+
AuthenticationWebFilter webFilter = (AuthenticationWebFilter) filters
186184
.filter((f) -> f instanceof AuthenticationWebFilter).findFirst()
187185
.orElse(null);
188186
ReactiveAuthenticationManager authenticationManager = (ReactiveAuthenticationManager) ReflectionTestUtils

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/servlet/OAuth2ResourceServerAutoConfigurationTests.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter;
4747
import org.springframework.security.web.FilterChainProxy;
4848
import org.springframework.security.web.SecurityFilterChain;
49-
import org.springframework.test.util.ReflectionTestUtils;
5049

5150
import static org.assertj.core.api.Assertions.assertThat;
5251
import static org.mockito.Mockito.mock;
@@ -158,13 +157,11 @@ public void autoConfigurationShouldBeConditionalOnJwtDecoderClass() {
158157
.run((context) -> assertThat(getBearerTokenFilter(context)).isNull());
159158
}
160159

161-
@SuppressWarnings("unchecked")
162160
private Filter getBearerTokenFilter(AssertableWebApplicationContext context) {
163161
FilterChainProxy filterChain = (FilterChainProxy) context
164162
.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN);
165163
List<SecurityFilterChain> filterChains = filterChain.getFilterChains();
166-
List<Filter> filters = (List<Filter>) ReflectionTestUtils
167-
.getField(filterChains.get(0), "filters");
164+
List<Filter> filters = filterChains.get(0).getFilters();
168165
return filters.stream()
169166
.filter((f) -> f instanceof BearerTokenAuthenticationFilter).findFirst()
170167
.orElse(null);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizerTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
4141
import org.springframework.mock.env.MockEnvironment;
4242
import org.springframework.test.context.support.TestPropertySourceUtils;
43-
import org.springframework.test.util.ReflectionTestUtils;
4443

4544
import static org.assertj.core.api.Assertions.assertThat;
4645
import static org.mockito.Mockito.mock;
@@ -172,8 +171,10 @@ public void customMaxHttpHeaderSizeIgnoredIfZero() {
172171

173172
private List<Integer> getRequestHeaderSizes(JettyWebServer server) {
174173
List<Integer> requestHeaderSizes = new ArrayList<>();
175-
Connector[] connectors = (Connector[]) ReflectionTestUtils.getField(server,
176-
"connectors");
174+
// Start (and directly stop) server to have connectors available
175+
server.start();
176+
server.stop();
177+
Connector[] connectors = server.getServer().getConnectors();
177178
for (Connector connector : connectors) {
178179
connector.getConnectionFactories().stream()
179180
.filter((factory) -> factory instanceof ConnectionFactory)

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616

1717
package org.springframework.boot.autoconfigure.web.embedded;
1818

19-
import java.util.Map;
2019
import java.util.function.Consumer;
2120

2221
import org.apache.catalina.Context;
2322
import org.apache.catalina.Valve;
24-
import org.apache.catalina.mapper.Mapper;
2523
import org.apache.catalina.startup.Tomcat;
2624
import org.apache.catalina.valves.AccessLogValve;
2725
import org.apache.catalina.valves.ErrorReportValve;
2826
import org.apache.catalina.valves.RemoteIpValve;
29-
import org.apache.catalina.webresources.StandardRoot;
3027
import org.apache.coyote.AbstractProtocol;
3128
import org.apache.coyote.http11.AbstractHttp11Protocol;
3229
import org.junit.Before;
@@ -40,7 +37,6 @@
4037
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
4138
import org.springframework.mock.env.MockEnvironment;
4239
import org.springframework.test.context.support.TestPropertySourceUtils;
43-
import org.springframework.test.util.ReflectionTestUtils;
4440
import org.springframework.util.unit.DataSize;
4541

4642
import static org.assertj.core.api.Assertions.assertThat;
@@ -180,18 +176,13 @@ public void customRemoteIpValve() {
180176
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
181177
}
182178

183-
@SuppressWarnings("unchecked")
184179
@Test
185180
public void customStaticResourceAllowCaching() {
186181
bind("server.tomcat.resource.allow-caching=false");
187182
customizeAndRunServer((server) -> {
188-
Mapper mapper = server.getTomcat().getService().getMapper();
189-
Object contextObjectToContextVersionMap = ReflectionTestUtils.getField(mapper,
190-
"contextObjectToContextVersionMap");
191-
Object tomcatEmbeddedContext = ((Map<Context, Object>) contextObjectToContextVersionMap)
192-
.values().toArray()[0];
193-
assertThat(((StandardRoot) ReflectionTestUtils.getField(tomcatEmbeddedContext,
194-
"resources")).isCachingAllowed()).isFalse();
183+
Tomcat tomcat = server.getTomcat();
184+
Context context = (Context) tomcat.getHost().findChildren()[0];
185+
assertThat(context.getResources().isCachingAllowed()).isFalse();
195186
});
196187
}
197188

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestWebDriverIntegrationTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
3030
import org.springframework.security.test.context.support.WithMockUser;
3131
import org.springframework.test.context.junit4.SpringRunner;
32-
import org.springframework.test.util.ReflectionTestUtils;
3332

3433
import static org.assertj.core.api.Assertions.assertThat;
3534
import static org.junit.Assert.fail;
@@ -64,7 +63,7 @@ public void shouldBeADifferentWebClient() {
6463
WebElement element = this.webDriver.findElement(By.tagName("body"));
6564
assertThat(element.getText()).isEqualTo("Hello");
6665
try {
67-
ReflectionTestUtils.invokeMethod(previousWebDriver, "getCurrentWindow");
66+
previousWebDriver.getWindowHandle();
6867
fail("Did not call quit()");
6968
}
7069
catch (NoSuchWindowException ex) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import java.nio.charset.StandardCharsets;
2424
import java.time.Duration;
2525
import java.util.Arrays;
26+
import java.util.HashMap;
2627
import java.util.Locale;
2728
import java.util.Map;
28-
import java.util.Set;
2929

3030
import javax.naming.InitialContext;
3131
import javax.naming.NamingException;
@@ -49,6 +49,7 @@
4949
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
5050
import org.apache.jasper.servlet.JspServlet;
5151
import org.apache.tomcat.JarScanFilter;
52+
import org.apache.tomcat.JarScanType;
5253
import org.junit.After;
5354
import org.junit.Rule;
5455
import org.junit.Test;
@@ -399,7 +400,6 @@ public void sessionIdGeneratorIsConfiguredWithAttributesFromTheManager() {
399400
}
400401

401402
@Test
402-
@SuppressWarnings("unchecked")
403403
public void tldSkipPatternsShouldBeAppliedToContextJarScanner() {
404404
TomcatServletWebServerFactory factory = getFactory();
405405
factory.addTldSkipPatterns("foo.jar", "bar.jar");
@@ -408,9 +408,9 @@ public void tldSkipPatternsShouldBeAppliedToContextJarScanner() {
408408
Tomcat tomcat = ((TomcatWebServer) this.webServer).getTomcat();
409409
Context context = (Context) tomcat.getHost().findChildren()[0];
410410
JarScanFilter jarScanFilter = context.getJarScanner().getJarScanFilter();
411-
Set<String> tldSkipSet = (Set<String>) ReflectionTestUtils.getField(jarScanFilter,
412-
"tldSkipSet");
413-
assertThat(tldSkipSet).contains("foo.jar", "bar.jar");
411+
assertThat(jarScanFilter.check(JarScanType.TLD, "foo.jar")).isFalse();
412+
assertThat(jarScanFilter.check(JarScanType.TLD, "bar.jar")).isFalse();
413+
assertThat(jarScanFilter.check(JarScanType.TLD, "test.jar")).isTrue();
414414
}
415415

416416
@Test
@@ -463,13 +463,15 @@ protected JspServlet getJspServlet() throws ServletException {
463463
return (JspServlet) standardWrapper.getServlet();
464464
}
465465

466-
@SuppressWarnings("unchecked")
467466
@Override
468467
protected Map<String, String> getActualMimeMappings() {
469468
Context context = (Context) ((TomcatWebServer) this.webServer).getTomcat()
470469
.getHost().findChildren()[0];
471-
return (Map<String, String>) ReflectionTestUtils.getField(context,
472-
"mimeMappings");
470+
Map<String, String> mimeMappings = new HashMap<>();
471+
for (String extension : context.findMimeMappings()) {
472+
mimeMappings.put(extension, context.findMimeMapping(extension));
473+
}
474+
return mimeMappings;
473475
}
474476

475477
@Override

0 commit comments

Comments
 (0)