Avoid reflection in tests

See gh-27085
pull/27157/head
dreis2211 3 years ago committed by Stephane Nicoll
parent b011e4ff97
commit c852961011

@ -20,6 +20,7 @@ import java.util.List;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.internal.MongoClientImpl;
import com.mongodb.connection.ConnectionPoolSettings;
import com.mongodb.event.ConnectionPoolListener;
import io.micrometer.core.instrument.binder.mongodb.DefaultMongoCommandTagsProvider;
@ -176,18 +177,15 @@ class MongoMetricsAutoConfigurationTests {
private MongoClientSettings getActualMongoClientSettingsUsedToConstructClient(
final AssertableApplicationContext context) {
final MongoClient mongoClient = context.getBean(MongoClient.class);
return (MongoClientSettings) ReflectionTestUtils.getField(mongoClient, "settings");
final MongoClientImpl mongoClient = (MongoClientImpl) context.getBean(MongoClient.class);
return mongoClient.getSettings();
}
private List<ConnectionPoolListener> getConnectionPoolListenersFromClient(
final AssertableApplicationContext context) {
MongoClientSettings mongoClientSettings = getActualMongoClientSettingsUsedToConstructClient(context);
ConnectionPoolSettings connectionPoolSettings = mongoClientSettings.getConnectionPoolSettings();
@SuppressWarnings("unchecked")
List<ConnectionPoolListener> listeners = (List<ConnectionPoolListener>) ReflectionTestUtils
.getField(connectionPoolSettings, "connectionPoolListeners");
return listeners;
return connectionPoolSettings.getConnectionPoolListeners();
}
private MongoCommandTagsProvider getMongoCommandTagsProviderUsedToConstructListener(

@ -399,7 +399,7 @@ class RedisAutoConfigurationTests {
}
private LettucePoolingClientConfiguration getPoolingClientConfiguration(LettuceConnectionFactory factory) {
return (LettucePoolingClientConfiguration) ReflectionTestUtils.getField(factory, "clientConfiguration");
return (LettucePoolingClientConfiguration) factory.getClientConfiguration();
}
private String getUserName(LettuceConnectionFactory factory) {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-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.
@ -33,7 +33,6 @@ import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageC
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@ -143,10 +142,9 @@ class HttpMessageConvertersTests {
MappingJackson2HttpMessageConverter.class, MappingJackson2SmileHttpMessageConverter.class);
}
@SuppressWarnings("unchecked")
private List<HttpMessageConverter<?>> extractFormPartConverters(List<HttpMessageConverter<?>> converters) {
AllEncompassingFormHttpMessageConverter formConverter = findFormConverter(converters);
return (List<HttpMessageConverter<?>>) ReflectionTestUtils.getField(formConverter, "partConverters");
return formConverter.getPartConverters();
}
private AllEncompassingFormHttpMessageConverter findFormConverter(Collection<HttpMessageConverter<?>> converters) {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@ -21,6 +21,7 @@ import java.util.concurrent.TimeUnit;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.internal.MongoClientImpl;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -28,7 +29,6 @@ import org.springframework.boot.test.context.assertj.AssertableApplicationContex
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -86,8 +86,8 @@ class MongoAutoConfigurationTests {
private MongoClientSettings getSettings(AssertableApplicationContext context) {
assertThat(context).hasSingleBean(MongoClient.class);
MongoClient client = context.getBean(MongoClient.class);
return (MongoClientSettings) ReflectionTestUtils.getField(client, "settings");
MongoClientImpl client = (MongoClientImpl) context.getBean(MongoClient.class);
return client.getSettings();
}
@Configuration(proxyBeanMethods = false)

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@ -20,8 +20,7 @@ import java.util.List;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.client.internal.MongoClientImpl;
/**
* Tests for {@link MongoClientFactory}.
@ -42,7 +41,7 @@ class MongoClientFactoryTests extends MongoClientFactorySupportTests<MongoClient
@Override
protected MongoClientSettings getClientSettings(MongoClient client) {
return (MongoClientSettings) ReflectionTestUtils.getField(client, "settings");
return ((MongoClientImpl) client).getSettings();
}
}

@ -26,6 +26,7 @@ import com.mongodb.connection.StreamFactory;
import com.mongodb.connection.StreamFactoryFactory;
import com.mongodb.connection.netty.NettyStreamFactoryFactory;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.internal.MongoClientImpl;
import io.netty.channel.EventLoopGroup;
import org.junit.jupiter.api.Test;
@ -111,8 +112,8 @@ class MongoReactiveAutoConfigurationTests {
}
private MongoClientSettings getSettings(ApplicationContext context) {
MongoClient client = context.getBean(MongoClient.class);
return (MongoClientSettings) ReflectionTestUtils.getField(client, "settings");
MongoClientImpl client = (MongoClientImpl) context.getBean(MongoClient.class);
return client.getSettings();
}
@Configuration(proxyBeanMethods = false)

@ -20,8 +20,7 @@ import java.util.List;
import com.mongodb.MongoClientSettings;
import com.mongodb.reactivestreams.client.MongoClient;
import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.reactivestreams.client.internal.MongoClientImpl;
/**
* Tests for {@link ReactiveMongoClientFactory}.
@ -40,7 +39,7 @@ class ReactiveMongoClientFactoryTests extends MongoClientFactorySupportTests<Mon
@Override
protected MongoClientSettings getClientSettings(MongoClient client) {
return (MongoClientSettings) ReflectionTestUtils.getField(client, "settings");
return ((MongoClientImpl) client).getSettings();
}
}

@ -42,7 +42,7 @@ import org.apache.tomcat.util.net.AbstractEndpoint;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.junit.jupiter.api.Test;
import reactor.netty.http.HttpDecoderSpec;
import reactor.netty.http.server.HttpRequestDecoderSpec;
@ -422,11 +422,11 @@ class ServerPropertiesTests {
void jettyThreadPoolPropertyDefaultsShouldMatchServerDefault() {
JettyServletWebServerFactory jettyFactory = new JettyServletWebServerFactory(0);
JettyWebServer jetty = (JettyWebServer) jettyFactory.getWebServer();
Server server = (Server) ReflectionTestUtils.getField(jetty, "server");
ThreadPool threadPool = (ThreadPool) ReflectionTestUtils.getField(server, "_threadPool");
int idleTimeout = (int) ReflectionTestUtils.getField(threadPool, "_idleTimeout");
int maxThreads = (int) ReflectionTestUtils.getField(threadPool, "_maxThreads");
int minThreads = (int) ReflectionTestUtils.getField(threadPool, "_minThreads");
Server server = jetty.getServer();
QueuedThreadPool threadPool = (QueuedThreadPool) server.getThreadPool();
int idleTimeout = threadPool.getIdleTimeout();
int maxThreads = threadPool.getMaxThreads();
int minThreads = threadPool.getMinThreads();
assertThat(this.properties.getJetty().getThreads().getIdleTimeout().toMillis()).isEqualTo(idleTimeout);
assertThat(this.properties.getJetty().getThreads().getMax()).isEqualTo(maxThreads);
assertThat(this.properties.getJetty().getThreads().getMin()).isEqualTo(minThreads);

@ -696,8 +696,8 @@ class WebMvcAutoConfigurationTests {
this.contextRunner.withPropertyValues(prefix + "static-locations:classpath:/welcome-page/")
.withUserConfiguration(CorsConfigurer.class).run((context) -> {
WelcomePageHandlerMapping bean = context.getBean(WelcomePageHandlerMapping.class);
UrlBasedCorsConfigurationSource source = (UrlBasedCorsConfigurationSource) ReflectionTestUtils
.getField(bean, "corsConfigurationSource");
UrlBasedCorsConfigurationSource source = (UrlBasedCorsConfigurationSource) bean
.getCorsConfigurationSource();
assertThat(source.getCorsConfigurations()).containsKey("/**");
});
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
@ -55,7 +55,6 @@ import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandler;
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
import org.springframework.stereotype.Controller;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration;
@ -138,13 +137,10 @@ class WebSocketMessagingAutoConfigurationTests {
return customizedConverters;
}
@SuppressWarnings("unchecked")
private List<MessageConverter> getDefaultConverters() {
DelegatingWebSocketMessageBrokerConfiguration configuration = new DelegatingWebSocketMessageBrokerConfiguration();
// We shouldn't usually call this method directly since it's on a non-proxy config
CompositeMessageConverter compositeDefaultConverter = ReflectionTestUtils.invokeMethod(configuration,
"brokerMessageConverter");
return (List<MessageConverter>) ReflectionTestUtils.getField(compositeDefaultConverter, "converters");
CompositeMessageConverter compositeDefaultConverter = configuration.brokerMessageConverter();
return compositeDefaultConverter.getConverters();
}
private Object performStompSubscription(String topic) throws Throwable {

Loading…
Cancel
Save