Browse Source

Polish ExceptionHandlerTests

pull/25798/head
Sam Brannen 4 years ago
parent
commit
40fc472c70
  1. 20
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java

20
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,14 +30,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
/** /**
* Exception handling via {@code @ExceptionHandler} method. * Exception handling via {@code @ExceptionHandler} methods.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen
*/ */
public class ExceptionHandlerTests { class ExceptionHandlerTests {
@Test @Test
public void testExceptionHandlerMethod() throws Exception { void localExceptionHandlerMethod() throws Exception {
standaloneSetup(new PersonController()).build() standaloneSetup(new PersonController()).build()
.perform(get("/person/Clyde")) .perform(get("/person/Clyde"))
.andExpect(status().isOk()) .andExpect(status().isOk())
@ -45,7 +46,7 @@ public class ExceptionHandlerTests {
} }
@Test @Test
public void testGlobalExceptionHandlerMethod() throws Exception { void globalExceptionHandlerMethod() throws Exception {
standaloneSetup(new PersonController()).setControllerAdvice(new GlobalExceptionHandler()).build() standaloneSetup(new PersonController()).setControllerAdvice(new GlobalExceptionHandler()).build()
.perform(get("/person/Bonnie")) .perform(get("/person/Bonnie"))
.andExpect(status().isOk()) .andExpect(status().isOk())
@ -53,7 +54,7 @@ public class ExceptionHandlerTests {
} }
@Test @Test
public void testGlobalExceptionHandlerMethodUsingClassArgument() throws Exception { void globalExceptionHandlerMethodUsingClassArgument() throws Exception {
standaloneSetup(PersonController.class).setControllerAdvice(GlobalExceptionHandler.class).build() standaloneSetup(PersonController.class).setControllerAdvice(GlobalExceptionHandler.class).build()
.perform(get("/person/Bonnie")) .perform(get("/person/Bonnie"))
.andExpect(status().isOk()) .andExpect(status().isOk())
@ -65,7 +66,7 @@ public class ExceptionHandlerTests {
private static class PersonController { private static class PersonController {
@GetMapping("/person/{name}") @GetMapping("/person/{name}")
public String show(@PathVariable String name) { String show(@PathVariable String name) {
if (name.equals("Clyde")) { if (name.equals("Clyde")) {
throw new IllegalArgumentException("simulated exception"); throw new IllegalArgumentException("simulated exception");
} }
@ -76,7 +77,7 @@ public class ExceptionHandlerTests {
} }
@ExceptionHandler @ExceptionHandler
public String handleException(IllegalArgumentException exception) { String handleException(IllegalArgumentException exception) {
return "errorView"; return "errorView";
} }
} }
@ -86,10 +87,9 @@ public class ExceptionHandlerTests {
private static class GlobalExceptionHandler { private static class GlobalExceptionHandler {
@ExceptionHandler @ExceptionHandler
public String handleException(IllegalStateException exception) { String handleException(IllegalStateException exception) {
return "globalErrorView"; return "globalErrorView";
} }
} }
} }

Loading…
Cancel
Save