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-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";
}
}
}