21
21
import org .springframework .mock .web .MockHttpServletResponse ;
22
22
import org .springframework .test .web .servlet .StubMvcResult ;
23
23
24
+ import static org .assertj .core .api .Assertions .assertThatCode ;
24
25
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
25
26
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .forwardedUrl ;
26
27
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .forwardedUrlPattern ;
@@ -39,99 +40,112 @@ public class MockMvcResultMatchersTests {
39
40
40
41
@ Test
41
42
public void redirect () throws Exception {
42
- redirectedUrl ("/resource/1" ).match (getRedirectedUrlStubMvcResult ("/resource/1" ));
43
+ assertThatCode (() -> redirectedUrl ("/resource/1" ).match (redirectedUrlStub ("/resource/1" )))
44
+ .doesNotThrowAnyException ();
43
45
}
44
46
45
47
@ Test
46
- public void redirectNonMatching () {
47
- assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
48
- redirectedUrl ("/resource/2" ).match (getRedirectedUrlStubMvcResult ("/resource/1" )));
48
+ public void redirectNonMatching () throws Exception {
49
+ assertThatExceptionOfType (AssertionError .class )
50
+ .isThrownBy (() -> redirectedUrl ("/resource/2" ).match (redirectedUrlStub ("/resource/1" )))
51
+ .withMessageEndingWith ("expected:</resource/2> but was:</resource/1>" );
49
52
}
50
53
51
54
@ Test
52
- public void redirectNonMatchingBecauseNotRedirect () {
53
- assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
54
- redirectedUrl ("/resource/1" ).match (getForwardedUrlStubMvcResult ("/resource/1" )));
55
+ public void redirectNonMatchingBecauseNotRedirect () throws Exception {
56
+ assertThatExceptionOfType (AssertionError .class )
57
+ .isThrownBy (() -> redirectedUrl ("/resource/1" ).match (forwardedUrlStub ("/resource/1" )))
58
+ .withMessageEndingWith ("expected:</resource/1> but was:<null>" );
55
59
}
56
60
57
61
@ Test
58
62
public void redirectWithUrlTemplate () throws Exception {
59
- redirectedUrlTemplate ("/orders/{orderId}/items/{itemId}" , 1 , 2 ).match (getRedirectedUrlStubMvcResult ("/orders/1/items/2" ));
63
+ assertThatCode (() -> redirectedUrlTemplate ("/orders/{orderId}/items/{itemId}" , 1 , 2 ).match (redirectedUrlStub ("/orders/1/items/2" )))
64
+ .doesNotThrowAnyException ();
60
65
}
61
66
62
67
@ Test
63
68
public void redirectWithMatchingPattern () throws Exception {
64
- redirectedUrlPattern ("/resource/*" ).match (getRedirectedUrlStubMvcResult ("/resource/1" ));
69
+ assertThatCode (() -> redirectedUrlPattern ("/resource/*" ).match (redirectedUrlStub ("/resource/1" )))
70
+ .doesNotThrowAnyException ();
65
71
}
66
72
67
73
@ Test
68
74
public void redirectWithNonMatchingPattern () throws Exception {
69
- assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
70
- redirectedUrlPattern ("/resource/" ).match (getRedirectedUrlStubMvcResult ("/resource/1" )));
75
+ assertThatExceptionOfType (AssertionError .class )
76
+ .isThrownBy (() -> redirectedUrlPattern ("/resource/" ).match (redirectedUrlStub ("/resource/1" )))
77
+ .withMessage ("'/resource/' is not an Ant-style path pattern" );
71
78
}
72
79
73
80
@ Test
74
81
public void redirectWithNonMatchingPatternBecauseNotRedirect () {
75
- assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
76
- redirectedUrlPattern ("/resource/*" ).match (getForwardedUrlStubMvcResult ("/resource/1" )));
82
+ assertThatExceptionOfType (AssertionError .class )
83
+ .isThrownBy (() -> redirectedUrlPattern ("/resource/*" ).match (forwardedUrlStub ("/resource/1" )))
84
+ .withMessage ("Redirected URL 'null' does not match the expected URL pattern '/resource/*'" );
77
85
}
78
86
79
87
@ Test
80
88
public void forward () throws Exception {
81
- forwardedUrl ("/api/resource/1" ).match (getForwardedUrlStubMvcResult ("/api/resource/1" ));
89
+ assertThatCode (() -> forwardedUrl ("/api/resource/1" ).match (forwardedUrlStub ("/api/resource/1" )))
90
+ .doesNotThrowAnyException ();
82
91
}
83
92
84
93
@ Test
85
94
public void forwardNonMatching () {
86
- assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
87
- forwardedUrlPattern ("api/resource/2" ).match (getForwardedUrlStubMvcResult ("api/resource/1" )));
95
+ assertThatExceptionOfType (AssertionError .class )
96
+ .isThrownBy (() -> forwardedUrlPattern ("api/resource/2" ).match (forwardedUrlStub ("api/resource/1" )))
97
+ .withMessage ("'api/resource/2' is not an Ant-style path pattern" );
88
98
}
89
99
90
100
@ Test
91
101
public void forwardNonMatchingBecauseNotForward () {
92
- assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
93
- forwardedUrlPattern ("api/resource/1" ).match (getRedirectedUrlStubMvcResult ("api/resource/1" )));
102
+ assertThatExceptionOfType (AssertionError .class )
103
+ .isThrownBy (() -> forwardedUrlPattern ("/resource/*" ).match (redirectedUrlStub ("/resource/1" )))
104
+ .withMessage ("Forwarded URL 'null' does not match the expected URL pattern '/resource/*'" );
94
105
}
95
106
96
107
@ Test
97
108
public void forwardWithQueryString () throws Exception {
98
- forwardedUrl ("/api/resource/1?arg=value" ).match (getForwardedUrlStubMvcResult ("/api/resource/1?arg=value" ));
109
+ assertThatCode (() -> forwardedUrl ("/api/resource/1?arg=value" ).match (forwardedUrlStub ("/api/resource/1?arg=value" )))
110
+ .doesNotThrowAnyException ();
99
111
}
100
112
101
113
@ Test
102
114
public void forwardWithUrlTemplate () throws Exception {
103
- forwardedUrlTemplate ("/orders/{orderId}/items/{itemId}" , 1 , 2 ).match (getForwardedUrlStubMvcResult ("/orders/1/items/2" ));
115
+ assertThatCode (() -> forwardedUrlTemplate ("/orders/{orderId}/items/{itemId}" , 1 , 2 ).match (forwardedUrlStub ("/orders/1/items/2" )))
116
+ .doesNotThrowAnyException ();
104
117
}
105
118
106
119
@ Test
107
120
public void forwardWithMatchingPattern () throws Exception {
108
- forwardedUrlPattern ("/api/**/?" ).match (getForwardedUrlStubMvcResult ("/api/resource/1" ));
121
+ assertThatCode (() -> forwardedUrlPattern ("/api/**/?" ).match (forwardedUrlStub ("/api/resource/1" )))
122
+ .doesNotThrowAnyException ();
109
123
}
110
124
111
125
@ Test
112
126
public void forwardWithNonMatchingPattern () throws Exception {
113
- assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
114
- forwardedUrlPattern ("/resource/" ).match (getForwardedUrlStubMvcResult ("/resource/1" )));
127
+ assertThatExceptionOfType (AssertionError .class )
128
+ .isThrownBy (() -> forwardedUrlPattern ("/resource/" ).match (forwardedUrlStub ("/resource/1" )))
129
+ .withMessage ("'/resource/' is not an Ant-style path pattern" );
115
130
}
116
131
117
132
@ Test
118
133
public void forwardWithNonMatchingPatternBecauseNotForward () {
119
- assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
120
- forwardedUrlPattern ("/resource/*" ).match (getRedirectedUrlStubMvcResult ("/resource/1" )));
134
+ assertThatExceptionOfType (AssertionError .class )
135
+ .isThrownBy (() -> forwardedUrlPattern ("/resource/*" ).match (redirectedUrlStub ("/resource/1" )))
136
+ .withMessage ("Forwarded URL 'null' does not match the expected URL pattern '/resource/*'" );
121
137
}
122
138
123
- private StubMvcResult getRedirectedUrlStubMvcResult (String redirectUrl ) throws Exception {
139
+ private StubMvcResult redirectedUrlStub (String redirectUrl ) throws Exception {
124
140
MockHttpServletResponse response = new MockHttpServletResponse ();
125
141
response .sendRedirect (redirectUrl );
126
- StubMvcResult mvcResult = new StubMvcResult (null , null , null , null , null , null , response );
127
- return mvcResult ;
142
+ return new StubMvcResult (null , null , null , null , null , null , response );
128
143
}
129
144
130
- private StubMvcResult getForwardedUrlStubMvcResult (String forwardedUrl ) {
145
+ private StubMvcResult forwardedUrlStub (String forwardedUrl ) {
131
146
MockHttpServletResponse response = new MockHttpServletResponse ();
132
147
response .setForwardedUrl (forwardedUrl );
133
- StubMvcResult mvcResult = new StubMvcResult (null , null , null , null , null , null , response );
134
- return mvcResult ;
148
+ return new StubMvcResult (null , null , null , null , null , null , response );
135
149
}
136
150
137
151
}
0 commit comments