Browse Source

Polish HandlerResultMatchers

pull/1469/head
Rossen Stoyanchev 9 years ago
parent
commit
1320da906b
  1. 45
      spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java
  2. 19
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HandlerAssertionTests.java

45
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"); * 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.
@ -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.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.test.util.AssertionErrors.*; 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.
* <p>An instance of this class is typically accessed via * <p>An instance of this class is typically accessed via
* {@link MockMvcResultMatchers#handler}. * {@link MockMvcResultMatchers#handler}.
* *
* <p><strong>Note:</strong> 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 * @author Rossen Stoyanchev
* @since 3.2 * @since 3.2
*/ */
public class HandlerResultMatchers { public class HandlerResultMatchers {
/** /**
* Protected constructor. * Protected constructor.
* Use {@link MockMvcResultMatchers#handler()}. * Use {@link MockMvcResultMatchers#handler()}.
@ -67,56 +74,50 @@ public class HandlerResultMatchers {
} }
/** /**
* Assert the name of the controller method that processed the request with * Assert the name of the controller method used to process the request
* the given Hamcrest {@link Matcher}. * using the given Hamcrest {@link Matcher}.
* <p>Use of this method implies annotated controllers are processed with
* {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}.
*/ */
public ResultMatcher methodName(final Matcher<? super String> matcher) { public ResultMatcher methodName(final Matcher<? super String> matcher) {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Object handler = assertHandlerMethod(result); HandlerMethod handlerMethod = getHandlerMethod(result);
assertThat("HandlerMethod", ((HandlerMethod) handler).getMethod().getName(), matcher); assertThat("HandlerMethod", handlerMethod.getMethod().getName(), matcher);
} }
}; };
} }
/** /**
* Assert the name of the controller method that processed the request. * Assert the name of the controller method used to process the request.
* <p>Use of this method implies annotated controllers are processed with
* {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}.
*/ */
public ResultMatcher methodName(final String name) { public ResultMatcher methodName(final String name) {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Object handler = assertHandlerMethod(result); HandlerMethod handlerMethod = getHandlerMethod(result);
assertEquals("HandlerMethod", name, ((HandlerMethod) handler).getMethod().getName()); assertEquals("HandlerMethod", name, handlerMethod.getMethod().getName());
} }
}; };
} }
/** /**
* Assert the controller method that processed the request. * Assert the controller method used to process the request.
* <p>Use of this method implies annotated controllers are processed with
* {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}.
*/ */
public ResultMatcher method(final Method method) { public ResultMatcher method(final Method method) {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) throws Exception { public void match(MvcResult result) throws Exception {
Object handler = assertHandlerMethod(result); HandlerMethod handlerMethod = getHandlerMethod(result);
assertEquals("HandlerMethod", method, ((HandlerMethod) handler).getMethod()); assertEquals("HandlerMethod", method, handlerMethod.getMethod());
} }
}; };
} }
private static Object assertHandlerMethod(MvcResult result) { private static HandlerMethod getHandlerMethod(MvcResult result) {
Object handler = result.getHandler(); Object handler = result.getHandler();
assertTrue("No handler: ", handler != null); assertTrue("No handler: ", handler != null);
assertTrue("Not a HandlerMethod: " + handler, HandlerMethod.class.isInstance(handler)); assertTrue("Not a HandlerMethod: " + handler, HandlerMethod.class.isInstance(handler));
return handler; return (HandlerMethod) handler;
} }
} }

19
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"); * 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.
@ -21,20 +21,21 @@ import java.lang.reflect.Method;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 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.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; 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. * Examples of expectations on the controller type and controller method.
*
* <p>Note that in most cases "handler" is synonymous with "controller".
* For example an {@code @Controller} is a kind of handler.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
*/ */
@ -72,12 +73,14 @@ public class HandlerAssertionTests {
} }
@Controller @Controller
private static class SimpleController { static class SimpleController {
@RequestMapping("/") @RequestMapping("/")
public String handle() { @ResponseBody
return "view"; public ResponseEntity<Void> handle() {
return ResponseEntity.ok().build();
} }
} }
} }

Loading…
Cancel
Save