|
|
|
@ -1,5 +1,5 @@
@@ -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;
@@ -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. |
|
|
|
|
* <p>An instance of this class is typically accessed via |
|
|
|
|
* {@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 |
|
|
|
|
* @since 3.2 |
|
|
|
|
*/ |
|
|
|
|
public class HandlerResultMatchers { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Protected constructor. |
|
|
|
|
* Use {@link MockMvcResultMatchers#handler()}. |
|
|
|
@ -67,56 +74,50 @@ public class HandlerResultMatchers {
@@ -67,56 +74,50 @@ public class HandlerResultMatchers {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Assert the name of the controller method that processed the request with |
|
|
|
|
* the given Hamcrest {@link Matcher}. |
|
|
|
|
* <p>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<? super String> 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. |
|
|
|
|
* <p>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. |
|
|
|
|
* <p>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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|