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