Skip to content

Commit 1320da9

Browse files
committed
Polish HandlerResultMatchers
1 parent 74c07d3 commit 1320da9

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,19 +27,26 @@
2727
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
2828
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
2929

30-
import static org.hamcrest.MatcherAssert.*;
31-
import static org.springframework.test.util.AssertionErrors.*;
30+
import static org.hamcrest.MatcherAssert.assertThat;
31+
import static org.springframework.test.util.AssertionErrors.assertEquals;
32+
import static org.springframework.test.util.AssertionErrors.assertTrue;
3233

3334
/**
34-
* Factory for assertions on the selected handler.
35+
* Factory for assertions on the selected handler or handler method.
3536
* <p>An instance of this class is typically accessed via
3637
* {@link MockMvcResultMatchers#handler}.
3738
*
39+
* <p><strong>Note:</strong> Expectations that assert the controller method
40+
* used to process the request work only for requests processed with
41+
* {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}
42+
* which is used by default with the Spring MVC Java config and XML namespace.
43+
*
3844
* @author Rossen Stoyanchev
3945
* @since 3.2
4046
*/
4147
public class HandlerResultMatchers {
4248

49+
4350
/**
4451
* Protected constructor.
4552
* Use {@link MockMvcResultMatchers#handler()}.
@@ -67,56 +74,50 @@ public void match(MvcResult result) throws Exception {
6774
}
6875

6976
/**
70-
* Assert the name of the controller method that processed the request with
71-
* the given Hamcrest {@link Matcher}.
72-
* <p>Use of this method implies annotated controllers are processed with
73-
* {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}.
77+
* Assert the name of the controller method used to process the request
78+
* using the given Hamcrest {@link Matcher}.
7479
*/
7580
public ResultMatcher methodName(final Matcher<? super String> matcher) {
7681
return new ResultMatcher() {
7782
@Override
7883
public void match(MvcResult result) throws Exception {
79-
Object handler = assertHandlerMethod(result);
80-
assertThat("HandlerMethod", ((HandlerMethod) handler).getMethod().getName(), matcher);
84+
HandlerMethod handlerMethod = getHandlerMethod(result);
85+
assertThat("HandlerMethod", handlerMethod.getMethod().getName(), matcher);
8186
}
8287
};
8388
}
8489

8590
/**
86-
* Assert the name of the controller method that processed the request.
87-
* <p>Use of this method implies annotated controllers are processed with
88-
* {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}.
91+
* Assert the name of the controller method used to process the request.
8992
*/
9093
public ResultMatcher methodName(final String name) {
9194
return new ResultMatcher() {
9295
@Override
9396
public void match(MvcResult result) throws Exception {
94-
Object handler = assertHandlerMethod(result);
95-
assertEquals("HandlerMethod", name, ((HandlerMethod) handler).getMethod().getName());
97+
HandlerMethod handlerMethod = getHandlerMethod(result);
98+
assertEquals("HandlerMethod", name, handlerMethod.getMethod().getName());
9699
}
97100
};
98101
}
99102

100103
/**
101-
* Assert the controller method that processed the request.
102-
* <p>Use of this method implies annotated controllers are processed with
103-
* {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}.
104+
* Assert the controller method used to process the request.
104105
*/
105106
public ResultMatcher method(final Method method) {
106107
return new ResultMatcher() {
107108
@Override
108109
public void match(MvcResult result) throws Exception {
109-
Object handler = assertHandlerMethod(result);
110-
assertEquals("HandlerMethod", method, ((HandlerMethod) handler).getMethod());
110+
HandlerMethod handlerMethod = getHandlerMethod(result);
111+
assertEquals("HandlerMethod", method, handlerMethod.getMethod());
111112
}
112113
};
113114
}
114115

115-
private static Object assertHandlerMethod(MvcResult result) {
116+
private static HandlerMethod getHandlerMethod(MvcResult result) {
116117
Object handler = result.getHandler();
117118
assertTrue("No handler: ", handler != null);
118119
assertTrue("Not a HandlerMethod: " + handler, HandlerMethod.class.isInstance(handler));
119-
return handler;
120+
return (HandlerMethod) handler;
120121
}
121122

122123
}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,20 +21,21 @@
2121
import org.junit.Before;
2222
import org.junit.Test;
2323

24+
import org.springframework.http.HttpEntity;
25+
import org.springframework.http.ResponseEntity;
2426
import org.springframework.stereotype.Controller;
2527
import org.springframework.test.web.servlet.MockMvc;
2628
import org.springframework.web.bind.annotation.RequestMapping;
29+
import org.springframework.web.bind.annotation.ResponseBody;
2730

2831
import static org.hamcrest.Matchers.*;
2932
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
3033
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
3134
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
35+
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on;
3236

3337
/**
34-
* Examples of expectations on the handler or handler method that executed the request.
35-
*
36-
* <p>Note that in most cases "handler" is synonymous with "controller".
37-
* For example an {@code @Controller} is a kind of handler.
38+
* Examples of expectations on the controller type and controller method.
3839
*
3940
* @author Rossen Stoyanchev
4041
*/
@@ -72,12 +73,14 @@ public void testHandlerMethod() throws Exception {
7273
}
7374

7475

76+
7577
@Controller
76-
private static class SimpleController {
78+
static class SimpleController {
7779

7880
@RequestMapping("/")
79-
public String handle() {
80-
return "view";
81+
@ResponseBody
82+
public ResponseEntity<Void> handle() {
83+
return ResponseEntity.ok().build();
8184
}
8285
}
8386
}

0 commit comments

Comments
 (0)