From 1320da906bfd9eb4c28485e98d27685cbaf0e3a1 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 15 Jan 2016 12:34:04 -0500 Subject: [PATCH] Polish HandlerResultMatchers --- .../servlet/result/HandlerResultMatchers.java | 45 ++++++++++--------- .../resultmatchers/HandlerAssertionTests.java | 19 ++++---- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java index 982eacdfb5..e1b0b6e500 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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,19 +27,26 @@ import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; -import static org.hamcrest.MatcherAssert.*; -import static org.springframework.test.util.AssertionErrors.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.AssertionErrors.assertTrue; /** - * Factory for assertions on the selected handler. + * Factory for assertions on the selected handler or handler method. *

An instance of this class is typically accessed via * {@link MockMvcResultMatchers#handler}. * + *

Note: Expectations that assert the controller method + * used to process the request work only for requests processed with + * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter} + * which is used by default with the Spring MVC Java config and XML namespace. + * * @author Rossen Stoyanchev * @since 3.2 */ public class HandlerResultMatchers { + /** * Protected constructor. * Use {@link MockMvcResultMatchers#handler()}. @@ -67,56 +74,50 @@ public class HandlerResultMatchers { } /** - * Assert the name of the controller method that processed the request with - * the given Hamcrest {@link Matcher}. - *

Use of this method implies annotated controllers are processed with - * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}. + * Assert the name of the controller method used to process the request + * using the given Hamcrest {@link Matcher}. */ public ResultMatcher methodName(final Matcher matcher) { return new ResultMatcher() { @Override public void match(MvcResult result) throws Exception { - Object handler = assertHandlerMethod(result); - assertThat("HandlerMethod", ((HandlerMethod) handler).getMethod().getName(), matcher); + HandlerMethod handlerMethod = getHandlerMethod(result); + assertThat("HandlerMethod", handlerMethod.getMethod().getName(), matcher); } }; } /** - * Assert the name of the controller method that processed the request. - *

Use of this method implies annotated controllers are processed with - * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}. + * Assert the name of the controller method used to process the request. */ public ResultMatcher methodName(final String name) { return new ResultMatcher() { @Override public void match(MvcResult result) throws Exception { - Object handler = assertHandlerMethod(result); - assertEquals("HandlerMethod", name, ((HandlerMethod) handler).getMethod().getName()); + HandlerMethod handlerMethod = getHandlerMethod(result); + assertEquals("HandlerMethod", name, handlerMethod.getMethod().getName()); } }; } /** - * Assert the controller method that processed the request. - *

Use of this method implies annotated controllers are processed with - * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}. + * Assert the controller method used to process the request. */ public ResultMatcher method(final Method method) { return new ResultMatcher() { @Override public void match(MvcResult result) throws Exception { - Object handler = assertHandlerMethod(result); - assertEquals("HandlerMethod", method, ((HandlerMethod) handler).getMethod()); + HandlerMethod handlerMethod = getHandlerMethod(result); + assertEquals("HandlerMethod", method, handlerMethod.getMethod()); } }; } - private static Object assertHandlerMethod(MvcResult result) { + private static HandlerMethod getHandlerMethod(MvcResult result) { Object handler = result.getHandler(); assertTrue("No handler: ", handler != null); assertTrue("Not a HandlerMethod: " + handler, HandlerMethod.class.isInstance(handler)); - return handler; + return (HandlerMethod) handler; } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HandlerAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HandlerAssertionTests.java index 9d3b883d4e..364fa70e33 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HandlerAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HandlerAssertionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -21,20 +21,21 @@ import java.lang.reflect.Method; import org.junit.Before; import org.junit.Test; +import org.springframework.http.HttpEntity; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import static org.hamcrest.Matchers.*; 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 static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on; /** - * Examples of expectations on the handler or handler method that executed the request. - * - *

Note that in most cases "handler" is synonymous with "controller". - * For example an {@code @Controller} is a kind of handler. + * Examples of expectations on the controller type and controller method. * * @author Rossen Stoyanchev */ @@ -72,12 +73,14 @@ public class HandlerAssertionTests { } + @Controller - private static class SimpleController { + static class SimpleController { @RequestMapping("/") - public String handle() { - return "view"; + @ResponseBody + public ResponseEntity handle() { + return ResponseEntity.ok().build(); } } }