1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
import java .util .ArrayList ;
23
23
import java .util .List ;
24
24
25
+ import org .junit .Before ;
25
26
import org .junit .Test ;
26
27
import org .springframework .core .Conventions ;
27
28
import org .springframework .http .HttpStatus ;
30
31
import org .springframework .test .web .servlet .MvcResult ;
31
32
import org .springframework .test .web .servlet .ResultMatcher ;
32
33
import org .springframework .test .web .servlet .StubMvcResult ;
33
- import org .springframework .test .web .servlet .result .StatusResultMatchers ;
34
34
import org .springframework .util .ReflectionUtils ;
35
35
import org .springframework .util .StringUtils ;
36
36
41
41
*/
42
42
public class StatusResultMatchersTests {
43
43
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
+
44
56
@ Test
45
57
public void testHttpStatusCodeResultMatchers () throws Exception {
46
58
47
- StatusResultMatchers resultMatchers = new StatusResultMatchers ();
48
-
49
59
List <AssertionError > failures = new ArrayList <AssertionError >();
50
60
51
61
for (HttpStatus status : HttpStatus .values ()) {
52
62
MockHttpServletResponse response = new MockHttpServletResponse ();
53
63
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 );
57
65
try {
58
- ResultMatcher matcher = (ResultMatcher ) ReflectionUtils .invokeMethod (method , resultMatchers );
66
+ Method method = getMethodForHttpStatus (status );
67
+ ResultMatcher matcher = (ResultMatcher ) ReflectionUtils .invokeMethod (method , this .matchers );
59
68
try {
60
- MvcResult mvcResult = new StubMvcResult (new MockHttpServletRequest (), null , null , null , null , null , response );
61
69
matcher .match (mvcResult );
62
70
}
63
71
catch (AssertionError error ) {
64
72
failures .add (error );
65
73
}
66
74
}
67
75
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 );
69
77
}
70
78
}
71
79
@@ -74,9 +82,40 @@ public void testHttpStatusCodeResultMatchers() throws Exception {
74
82
}
75
83
}
76
84
77
- private String statusToMethodName (HttpStatus status ) throws NoSuchMethodException {
85
+ private Method getMethodForHttpStatus (HttpStatus status ) throws NoSuchMethodException {
78
86
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
+ }
80
119
}
81
120
82
121
}
0 commit comments