From 9be1d158a671a8b47dc658b8e5644117b8eb84db Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 19 May 2015 15:53:11 +0100 Subject: [PATCH] Don't use reflection to verify interceptor registration A recent change to the internals of RequestMappingHandlerMapping broke to Spring Mobile-related tests as they were using reflection to verify that the configuration had been applied correctly. This commit makes the tests more robust by using the Java API to access the interceptors for a request and verify that the expected interceptor is present. To further simplify the tests, the unnecessary use of an embedded servlet container factory has also been removed. --- .../DeviceResolverAutoConfigurationTests.java | 63 +++++++++---------- .../SitePreferenceAutoConfigurationTests.java | 63 +++++++++---------- 2 files changed, 58 insertions(+), 68 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/DeviceResolverAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/DeviceResolverAutoConfigurationTests.java index a5bc0c46a37..0fb169e6f98 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/DeviceResolverAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/DeviceResolverAutoConfigurationTests.java @@ -16,38 +16,36 @@ package org.springframework.boot.autoconfigure.mobile; -import java.lang.reflect.Field; -import java.util.List; - import org.junit.After; import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; -import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; -import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; -import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; -import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver; import org.springframework.mobile.device.DeviceResolverHandlerInterceptor; -import org.springframework.util.ReflectionUtils; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockServletContext; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import static org.hamcrest.Matchers.hasItemInArray; +import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThat; /** * Tests for {@link DeviceResolverAutoConfiguration}. * * @author Roy Clarkson + * @author Andy Wilkinson */ public class DeviceResolverAutoConfigurationTests { - private static final MockEmbeddedServletContainerFactory containerFactory = new MockEmbeddedServletContainerFactory(); - private AnnotationConfigWebApplicationContext context; @After @@ -74,41 +72,38 @@ public class DeviceResolverAutoConfigurationTests { } @Test - @SuppressWarnings("unchecked") public void deviceResolverHandlerInterceptorRegistered() throws Exception { - AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext(); - context.register(Config.class, WebMvcAutoConfiguration.class, + this.context = new AnnotationConfigWebApplicationContext(); + this.context.setServletContext(new MockServletContext()); + this.context.register(Config.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, DeviceResolverAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); - context.refresh(); - RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) context - .getBean("requestMappingHandlerMapping"); - Field interceptorsField = ReflectionUtils.findField( - RequestMappingHandlerMapping.class, "interceptors"); - interceptorsField.setAccessible(true); - List interceptors = (List) ReflectionUtils.getField( - interceptorsField, mapping); - context.close(); - for (Object o : interceptors) { - if (o instanceof DeviceResolverHandlerInterceptor) { - return; - } - } - fail("DeviceResolverHandlerInterceptor was not registered."); + this.context.refresh(); + RequestMappingHandlerMapping mapping = this.context + .getBean(RequestMappingHandlerMapping.class); + HandlerInterceptor[] interceptors = mapping.getHandler( + new MockHttpServletRequest()).getInterceptors(); + assertThat(interceptors, + hasItemInArray(instanceOf(DeviceResolverHandlerInterceptor.class))); } @Configuration protected static class Config { @Bean - public EmbeddedServletContainerFactory containerFactory() { - return containerFactory; + public MyController controller() { + return new MyController(); } - @Bean - public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() { - return new EmbeddedServletContainerCustomizerBeanPostProcessor(); + } + + @Controller + protected static class MyController { + + @RequestMapping("/") + public void test() { + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/SitePreferenceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/SitePreferenceAutoConfigurationTests.java index 075690a994b..4b1717eee68 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/SitePreferenceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/SitePreferenceAutoConfigurationTests.java @@ -16,40 +16,38 @@ package org.springframework.boot.autoconfigure.mobile; -import java.lang.reflect.Field; -import java.util.List; - import org.junit.After; import org.junit.Test; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; -import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; -import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; -import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; -import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor; import org.springframework.mobile.device.site.SitePreferenceHandlerMethodArgumentResolver; -import org.springframework.util.ReflectionUtils; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockServletContext; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import static org.hamcrest.Matchers.hasItemInArray; +import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThat; /** * Tests for {@link SitePreferenceAutoConfiguration}. * * @author Roy Clarkson + * @author Andy Wilkinson */ public class SitePreferenceAutoConfigurationTests { - private static final MockEmbeddedServletContainerFactory containerFactory = new MockEmbeddedServletContainerFactory(); - private AnnotationConfigWebApplicationContext context; @After @@ -118,41 +116,38 @@ public class SitePreferenceAutoConfigurationTests { } @Test - @SuppressWarnings("unchecked") public void sitePreferenceHandlerInterceptorRegistered() throws Exception { - AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext(); - context.register(Config.class, WebMvcAutoConfiguration.class, + this.context = new AnnotationConfigWebApplicationContext(); + this.context.setServletContext(new MockServletContext()); + this.context.register(Config.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, SitePreferenceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); - context.refresh(); - RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) context - .getBean("requestMappingHandlerMapping"); - Field interceptorsField = ReflectionUtils.findField( - RequestMappingHandlerMapping.class, "interceptors"); - interceptorsField.setAccessible(true); - List interceptors = (List) ReflectionUtils.getField( - interceptorsField, mapping); - context.close(); - for (Object o : interceptors) { - if (o instanceof SitePreferenceHandlerInterceptor) { - return; - } - } - fail("SitePreferenceHandlerInterceptor was not registered."); + this.context.refresh(); + RequestMappingHandlerMapping mapping = this.context + .getBean(RequestMappingHandlerMapping.class); + HandlerInterceptor[] interceptors = mapping.getHandler( + new MockHttpServletRequest()).getInterceptors(); + assertThat(interceptors, + hasItemInArray(instanceOf(SitePreferenceHandlerInterceptor.class))); } @Configuration protected static class Config { @Bean - public EmbeddedServletContainerFactory containerFactory() { - return containerFactory; + public MyController controller() { + return new MyController(); } - @Bean - public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() { - return new EmbeddedServletContainerCustomizerBeanPostProcessor(); + } + + @Controller + protected static class MyController { + + @RequestMapping("/") + public void test() { + } }