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

19
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HandlerAssertionTests.java

@ -1,5 +1,5 @@ @@ -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; @@ -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.
*
* <p>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 { @@ -72,12 +73,14 @@ public class HandlerAssertionTests {
}
@Controller
private static class SimpleController {
static class SimpleController {
@RequestMapping("/")
public String handle() {
return "view";
@ResponseBody
public ResponseEntity<Void> handle() {
return ResponseEntity.ok().build();
}
}
}

Loading…
Cancel
Save