Skip to content

Commit b1abe26

Browse files
committed
Add ResultMatchers for status code ranges
Issue: SPR-11424
1 parent 206655c commit b1abe26

File tree

2 files changed

+122
-13
lines changed

2 files changed

+122
-13
lines changed

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -65,6 +65,76 @@ public void match(MvcResult result) throws Exception {
6565
};
6666
}
6767

68+
/**
69+
* Assert the response status code is in the 1xx range.
70+
*/
71+
public ResultMatcher is1xxInformational() {
72+
return new ResultMatcher() {
73+
@Override
74+
public void match(MvcResult result) throws Exception {
75+
assertEquals("Range for response status value " + result.getResponse().getStatus(),
76+
getHttpStatusSeries(result), HttpStatus.Series.INFORMATIONAL);
77+
}
78+
};
79+
}
80+
81+
/**
82+
* Assert the response status code is in the 2xx range.
83+
*/
84+
public ResultMatcher is2xxSuccessful() {
85+
return new ResultMatcher() {
86+
@Override
87+
public void match(MvcResult result) throws Exception {
88+
assertEquals("Range for response status value " + result.getResponse().getStatus(),
89+
getHttpStatusSeries(result), HttpStatus.Series.SUCCESSFUL);
90+
}
91+
};
92+
}
93+
94+
/**
95+
* Assert the response status code is in the 3xx range.
96+
*/
97+
public ResultMatcher is3xxRedirection() {
98+
return new ResultMatcher() {
99+
@Override
100+
public void match(MvcResult result) throws Exception {
101+
assertEquals("Range for response status value " + result.getResponse().getStatus(),
102+
getHttpStatusSeries(result), HttpStatus.Series.REDIRECTION);
103+
}
104+
};
105+
}
106+
107+
/**
108+
* Assert the response status code is in the 4xx range.
109+
*/
110+
public ResultMatcher is4xxClientError() {
111+
return new ResultMatcher() {
112+
@Override
113+
public void match(MvcResult result) throws Exception {
114+
assertEquals("Range for response status value " + result.getResponse().getStatus(),
115+
getHttpStatusSeries(result), HttpStatus.Series.CLIENT_ERROR);
116+
}
117+
};
118+
}
119+
120+
/**
121+
* Assert the response status code is in the 5xx range.
122+
*/
123+
public ResultMatcher is5xxServerError() {
124+
return new ResultMatcher() {
125+
@Override
126+
public void match(MvcResult result) throws Exception {
127+
assertEquals("Range for response status value " + result.getResponse().getStatus(),
128+
getHttpStatusSeries(result), HttpStatus.Series.SERVER_ERROR);
129+
}
130+
};
131+
}
132+
133+
private HttpStatus.Series getHttpStatusSeries(MvcResult result) {
134+
int statusValue = result.getResponse().getStatus();
135+
HttpStatus status = HttpStatus.valueOf(statusValue);
136+
return status.series();
137+
}
68138

69139
/**
70140
* Assert the Servlet response error message with the given Hamcrest {@link Matcher}.

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

Lines changed: 51 additions & 12 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-2014 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.
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424

25+
import org.junit.Before;
2526
import org.junit.Test;
2627
import org.springframework.core.Conventions;
2728
import org.springframework.http.HttpStatus;
@@ -30,7 +31,6 @@
3031
import org.springframework.test.web.servlet.MvcResult;
3132
import org.springframework.test.web.servlet.ResultMatcher;
3233
import org.springframework.test.web.servlet.StubMvcResult;
33-
import org.springframework.test.web.servlet.result.StatusResultMatchers;
3434
import org.springframework.util.ReflectionUtils;
3535
import org.springframework.util.StringUtils;
3636

@@ -41,31 +41,39 @@
4141
*/
4242
public class StatusResultMatchersTests {
4343

44+
private StatusResultMatchers matchers;
45+
46+
private MockHttpServletRequest request;
47+
48+
49+
@Before
50+
public void setup() {
51+
this.matchers = new StatusResultMatchers();
52+
this.request = new MockHttpServletRequest();
53+
}
54+
55+
4456
@Test
4557
public void testHttpStatusCodeResultMatchers() throws Exception {
4658

47-
StatusResultMatchers resultMatchers = new StatusResultMatchers();
48-
4959
List<AssertionError> failures = new ArrayList<AssertionError>();
5060

5161
for(HttpStatus status : HttpStatus.values()) {
5262
MockHttpServletResponse response = new MockHttpServletResponse();
5363
response.setStatus(status.value());
54-
55-
String methodName = statusToMethodName(status);
56-
Method method = StatusResultMatchers.class.getMethod(methodName);
64+
MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
5765
try {
58-
ResultMatcher matcher = (ResultMatcher) ReflectionUtils.invokeMethod(method, resultMatchers);
66+
Method method = getMethodForHttpStatus(status);
67+
ResultMatcher matcher = (ResultMatcher) ReflectionUtils.invokeMethod(method, this.matchers);
5968
try {
60-
MvcResult mvcResult = new StubMvcResult(new MockHttpServletRequest(), null, null, null, null, null, response);
6169
matcher.match(mvcResult);
6270
}
6371
catch (AssertionError error) {
6472
failures.add(error);
6573
}
6674
}
6775
catch (Exception ex) {
68-
throw new Exception("Failed to obtain ResultMatcher: " + method.toString(), ex);
76+
throw new Exception("Failed to obtain ResultMatcher for status " + status, ex);
6977
}
7078
}
7179

@@ -74,9 +82,40 @@ public void testHttpStatusCodeResultMatchers() throws Exception {
7482
}
7583
}
7684

77-
private String statusToMethodName(HttpStatus status) throws NoSuchMethodException {
85+
private Method getMethodForHttpStatus(HttpStatus status) throws NoSuchMethodException {
7886
String name = status.name().toLowerCase().replace("_", "-");
79-
return "is" + StringUtils.capitalize(Conventions.attributeNameToPropertyName(name));
87+
name = "is" + StringUtils.capitalize(Conventions.attributeNameToPropertyName(name));
88+
return StatusResultMatchers.class.getMethod(name);
89+
}
90+
91+
@Test
92+
public void statusRanges() throws Exception {
93+
94+
for(HttpStatus status : HttpStatus.values()) {
95+
96+
MockHttpServletResponse response = new MockHttpServletResponse();
97+
response.setStatus(status.value());
98+
MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
99+
switch (status.series().value()) {
100+
case 1:
101+
this.matchers.is1xxInformational().match(mvcResult);
102+
break;
103+
case 2:
104+
this.matchers.is2xxSuccessful().match(mvcResult);
105+
break;
106+
case 3:
107+
this.matchers.is3xxRedirection().match(mvcResult);
108+
break;
109+
case 4:
110+
this.matchers.is4xxClientError().match(mvcResult);
111+
break;
112+
case 5:
113+
this.matchers.is5xxServerError().match(mvcResult);
114+
break;
115+
default:
116+
fail("Unexpected range for status code value " + status);
117+
}
118+
}
80119
}
81120

82121
}

0 commit comments

Comments
 (0)