Support @ControllerAdvice in StandaloneMockMvcBuilder

Issue: SPR-12751
This commit is contained in:
Rossen Stoyanchev 2015-02-25 16:57:58 -05:00
parent cc33d3fac8
commit 2dd5875964
5 changed files with 94 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@ -27,6 +27,7 @@ import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContextAware;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.converter.HttpMessageConverter;
@ -86,6 +87,8 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
private final Object[] controllers;
private List<Object> controllerAdvice;
private List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
private List<HandlerMethodArgumentResolver> customArgumentResolvers = new ArrayList<HandlerMethodArgumentResolver>();
@ -100,7 +103,7 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
private FormattingConversionService conversionService = null;
private List<HandlerExceptionResolver> handlerExceptionResolvers = new ArrayList<HandlerExceptionResolver>();
private List<HandlerExceptionResolver> handlerExceptionResolvers;
private Long asyncRequestTimeout;
@ -128,6 +131,19 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
this.controllers = controllers;
}
/**
* Register {@link org.springframework.web.bind.annotation.ControllerAdvice
* ControllerAdvice} instances to be used with this MockMvc instance.
* <p>Normally {@code @ControllerAdvice} are auto-detected. However since the
* standalone setup does not load Spring configuration they need to be
* registered explicitly instead.
* @since 4.2
*/
public StandaloneMockMvcBuilder setControllerAdvice(Object... controllerAdvice) {
this.controllerAdvice = Arrays.asList(controllerAdvice);
return this;
}
/**
* Set the message converters to use in argument resolvers and in return value
* handlers, which support reading and/or writing to the body of the request
@ -317,6 +333,9 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
private void registerMvcSingletons(StubWebApplicationContext wac) {
StandaloneConfiguration config = new StandaloneConfiguration();
config.setApplicationContext(wac);
wac.addBeans(this.controllerAdvice);
StaticRequestMappingHandlerMapping hm = config.getHandlerMapping();
hm.setServletContext(wac.getServletContext());
@ -427,7 +446,23 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
@Override
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
exceptionResolvers.addAll(StandaloneMockMvcBuilder.this.handlerExceptionResolvers);
if (handlerExceptionResolvers == null) {
return;
}
for (HandlerExceptionResolver resolver : handlerExceptionResolvers) {
if (resolver instanceof ApplicationContextAware) {
((ApplicationContextAware) resolver).setApplicationContext(getApplicationContext());
}
if (resolver instanceof InitializingBean) {
try {
((InitializingBean) resolver).afterPropertiesSet();
}
catch (Exception ex) {
throw new IllegalStateException("Failure from afterPropertiesSet", ex);
}
}
exceptionResolvers.add(resolver);
}
}
}

View File

@ -137,6 +137,9 @@ class StubWebApplicationContext implements WebApplicationContext {
}
public void addBeans(List<?> beans) {
if (beans == null) {
return;
}
for (Object bean : beans) {
String name = bean.getClass().getName() + "#" + ObjectUtils.getIdentityHexString(bean);
this.beanFactory.addBean(name, bean);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
@ -16,18 +16,19 @@
package org.springframework.test.web.servlet.samples.standalone;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**
* Exception handling via {@code @ExceptionHandler} method.
*
@ -35,6 +36,7 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
*/
public class ExceptionHandlerTests {
@Test
public void testExceptionHandlerMethod() throws Exception {
standaloneSetup(new PersonController()).build()
@ -43,6 +45,14 @@ public class ExceptionHandlerTests {
.andExpect(forwardedUrl("errorView"));
}
@Test
public void testGlobalExceptionHandlerMethod() throws Exception {
standaloneSetup(new PersonController()).setControllerAdvice(new GlobalExceptionHandler()).build()
.perform(get("/person/Bonnie"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("globalErrorView"));
}
@Controller
private static class PersonController {
@ -50,7 +60,10 @@ public class ExceptionHandlerTests {
@RequestMapping(value="/person/{name}", method=RequestMethod.GET)
public String show(@PathVariable String name) {
if (name.equals("Clyde")) {
throw new IllegalArgumentException("Black listed");
throw new IllegalArgumentException("simulated exception");
}
else if (name.equals("Bonnie")) {
throw new IllegalStateException("simulated exception");
}
return "person/show";
}
@ -60,4 +73,15 @@ public class ExceptionHandlerTests {
return "errorView";
}
}
@ControllerAdvice
private static class GlobalExceptionHandler {
@ExceptionHandler
public String handleException(IllegalStateException exception) {
return "globalErrorView";
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
@ -26,6 +26,8 @@ import org.springframework.test.web.Person;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -43,6 +45,7 @@ public class ModelAssertionTests {
private MockMvc mockMvc;
@Before
public void setup() {
@ -51,6 +54,7 @@ public class ModelAssertionTests {
this.mockMvc = standaloneSetup(controller)
.defaultRequest(get("/"))
.alwaysExpect(status().isOk())
.setControllerAdvice(new ModelAttributeAdvice())
.build();
}
@ -60,7 +64,8 @@ public class ModelAssertionTests {
.andExpect(model().attribute("integer", 3))
.andExpect(model().attribute("string", "a string value"))
.andExpect(model().attribute("integer", equalTo(3))) // Hamcrest...
.andExpect(model().attribute("string", equalTo("a string value")));
.andExpect(model().attribute("string", equalTo("a string value")))
.andExpect(model().attribute("globalAttrName", equalTo("Global Attribute Value")));
}
@Test
@ -113,4 +118,13 @@ public class ModelAssertionTests {
}
}
@ControllerAdvice
private static class ModelAttributeAdvice {
@ModelAttribute("globalAttrName")
public String getAttribute() {
return "Global Attribute Value";
}
}
}

View File

@ -207,6 +207,10 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
this.applicationContext = applicationContext;
}
public ApplicationContext getApplicationContext() {
return this.applicationContext;
}
/**
* Set the {@link javax.servlet.ServletContext}, e.g. for resource handling,
* looking up file extensions, etc.
@ -216,6 +220,9 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
this.servletContext = servletContext;
}
public ServletContext getServletContext() {
return this.servletContext;
}
/**
* Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping