Use hasFieldOrPropertyWithValue where possible

Closes gh-15582
pull/15589/head
dreis2211 6 years ago committed by Stephane Nicoll
parent c667a7efe4
commit 0f5f6f15dc

@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.elasticsearch.rest; package org.springframework.boot.autoconfigure.elasticsearch.rest;
import java.lang.reflect.Field;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -33,7 +32,6 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer; import org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -71,11 +69,8 @@ public class RestClientAutoConfigurationTests {
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(RestClient.class); assertThat(context).hasSingleBean(RestClient.class);
RestClient restClient = context.getBean(RestClient.class); RestClient restClient = context.getBean(RestClient.class);
Field field = ReflectionUtils.findField(RestClient.class, assertThat(restClient)
"maxRetryTimeoutMillis"); .hasFieldOrPropertyWithValue("maxRetryTimeoutMillis", 42L);
ReflectionUtils.makeAccessible(field);
assertThat(ReflectionUtils.getField(field, restClient))
.isEqualTo(42L);
}); });
} }

@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.gson; package org.springframework.boot.autoconfigure.gson;
import java.lang.reflect.Field;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -180,15 +179,7 @@ public class GsonAutoConfigurationTests {
public void withoutLenient() { public void withoutLenient() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
Gson gson = context.getBean(Gson.class); Gson gson = context.getBean(Gson.class);
/* assertThat(gson).hasFieldOrPropertyWithValue("lenient", false);
* It seems that lenient setting not work in version 2.8.2. We get access to
* it via reflection
*/
Field lenientField = gson.getClass().getDeclaredField("lenient");
lenientField.setAccessible(true);
boolean lenient = lenientField.getBoolean(gson);
assertThat(lenient).isFalse();
}); });
} }
@ -197,15 +188,7 @@ public class GsonAutoConfigurationTests {
this.contextRunner.withPropertyValues("spring.gson.lenient:true") this.contextRunner.withPropertyValues("spring.gson.lenient:true")
.run((context) -> { .run((context) -> {
Gson gson = context.getBean(Gson.class); Gson gson = context.getBean(Gson.class);
/* assertThat(gson).hasFieldOrPropertyWithValue("lenient", true);
* It seems that lenient setting not work in version 2.8.2. We get
* access to it via reflection
*/
Field lenientField = gson.getClass().getDeclaredField("lenient");
lenientField.setAccessible(true);
boolean lenient = lenientField.getBoolean(gson);
assertThat(lenient).isTrue();
}); });
} }

@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.orm.jpa; package org.springframework.boot.autoconfigure.orm.jpa;
import java.lang.reflect.Field;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -232,11 +231,9 @@ public abstract class AbstractJpaAutoConfigurationTests {
.run((context) -> { .run((context) -> {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = context LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = context
.getBean(LocalContainerEntityManagerFactoryBean.class); .getBean(LocalContainerEntityManagerFactoryBean.class);
Field field = LocalContainerEntityManagerFactoryBean.class assertThat(entityManagerFactoryBean).hasFieldOrPropertyWithValue(
.getDeclaredField("persistenceUnitManager"); "persistenceUnitManager",
field.setAccessible(true); context.getBean(PersistenceUnitManager.class));
assertThat(field.get(entityManagerFactoryBean))
.isEqualTo(context.getBean(PersistenceUnitManager.class));
}); });
} }

@ -16,7 +16,6 @@
package org.springframework.boot; package org.springframework.boot;
import java.lang.reflect.Field;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -96,7 +95,6 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebEnvironment; import org.springframework.web.context.ConfigurableWebEnvironment;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
@ -285,24 +283,22 @@ public class SpringApplicationTests {
} }
@Test @Test
public void triggersConfigFileApplicationListenerBeforeBinding() throws Exception { public void triggersConfigFileApplicationListenerBeforeBinding() {
SpringApplication application = new SpringApplication(ExampleConfig.class); SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE); application.setWebApplicationType(WebApplicationType.NONE);
this.context = application.run("--spring.config.name=bindtoapplication"); this.context = application.run("--spring.config.name=bindtoapplication");
Field field = ReflectionUtils.findField(SpringApplication.class, "bannerMode"); assertThat(application).hasFieldOrPropertyWithValue("bannerMode",
field.setAccessible(true); Banner.Mode.OFF);
assertThat((Banner.Mode) field.get(application)).isEqualTo(Banner.Mode.OFF);
} }
@Test @Test
public void bindsSystemPropertyToSpringApplication() throws Exception { public void bindsSystemPropertyToSpringApplication() {
System.setProperty("spring.main.banner-mode", "off"); System.setProperty("spring.main.banner-mode", "off");
SpringApplication application = new SpringApplication(ExampleConfig.class); SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE); application.setWebApplicationType(WebApplicationType.NONE);
this.context = application.run(); this.context = application.run();
Field field = ReflectionUtils.findField(SpringApplication.class, "bannerMode"); assertThat(application).hasFieldOrPropertyWithValue("bannerMode",
field.setAccessible(true); Banner.Mode.OFF);
assertThat((Banner.Mode) field.get(application)).isEqualTo(Banner.Mode.OFF);
} }
@Test @Test

@ -16,7 +16,6 @@
package org.springframework.boot.web.servlet.context; package org.springframework.boot.web.servlet.context;
import java.lang.reflect.Field;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Properties; import java.util.Properties;
@ -55,7 +54,6 @@ import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.server.MockServletWebServerFactory; import org.springframework.boot.web.servlet.server.MockServletWebServerFactory;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
@ -129,17 +127,13 @@ public class ServletWebServerApplicationContextTests {
} }
@Test @Test
public void doesNotRegistersShutdownHook() throws Exception { public void doesNotRegistersShutdownHook() {
// See gh-314 for background. We no longer register the shutdown hook // See gh-314 for background. We no longer register the shutdown hook
// since it is really the callers responsibility. The shutdown hook could // since it is really the callers responsibility. The shutdown hook could
// also be problematic in a classic WAR deployment. // also be problematic in a classic WAR deployment.
addWebServerFactoryBean(); addWebServerFactoryBean();
this.context.refresh(); this.context.refresh();
Field shutdownHookField = AbstractApplicationContext.class assertThat(this.context).hasFieldOrPropertyWithValue("shutdownHook", null);
.getDeclaredField("shutdownHook");
shutdownHookField.setAccessible(true);
Object shutdownHook = shutdownHookField.get(this.context);
assertThat(shutdownHook).isNull();
} }
@Test @Test

Loading…
Cancel
Save