Skip to content

Commit e92cbe1

Browse files
committed
Improve failure messages for redirect/forward in MockMvc
1 parent 7779aa3 commit e92cbe1

File tree

2 files changed

+54
-38
lines changed

2 files changed

+54
-38
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -106,15 +106,16 @@ public static ResultMatcher forwardedUrlTemplate(String urlTemplate, Object... u
106106
* Asserts the request was forwarded to the given URL.
107107
* <p>This method accepts {@link org.springframework.util.AntPathMatcher}
108108
* patterns.
109-
* @param urlPattern an AntPath pattern to match against
109+
* @param urlPattern an Ant-style path pattern to match against
110110
* @since 4.0
111111
* @see org.springframework.util.AntPathMatcher
112112
*/
113113
public static ResultMatcher forwardedUrlPattern(String urlPattern) {
114114
return result -> {
115-
assertTrue("AntPath pattern", pathMatcher.isPattern(urlPattern));
115+
assertTrue("'" + urlPattern + "' is not an Ant-style path pattern",
116+
pathMatcher.isPattern(urlPattern));
116117
String url = result.getResponse().getForwardedUrl();
117-
assertTrue("Forwarded URL does not match the expected URL pattern",
118+
assertTrue("Forwarded URL '" + url + "' does not match the expected URL pattern '" + urlPattern + "'",
118119
(url != null && pathMatcher.match(urlPattern, url)));
119120
};
120121
}
@@ -144,15 +145,16 @@ public static ResultMatcher redirectedUrlTemplate(String urlTemplate, Object...
144145
* Asserts the request was redirected to the given URL.
145146
* <p>This method accepts {@link org.springframework.util.AntPathMatcher}
146147
* patterns.
147-
* @param urlPattern an AntPath pattern to match against
148+
* @param urlPattern an Ant-style path pattern to match against
148149
* @since 4.0
149150
* @see org.springframework.util.AntPathMatcher
150151
*/
151152
public static ResultMatcher redirectedUrlPattern(String urlPattern) {
152153
return result -> {
153-
assertTrue("No Ant-style path pattern", pathMatcher.isPattern(urlPattern));
154+
assertTrue("'" + urlPattern + "' is not an Ant-style path pattern",
155+
pathMatcher.isPattern(urlPattern));
154156
String url = result.getResponse().getRedirectedUrl();
155-
assertTrue("Redirected URL does not match the expected URL pattern",
157+
assertTrue("Redirected URL '" + url + "' does not match the expected URL pattern '" + urlPattern + "'",
156158
(url != null && pathMatcher.match(urlPattern, url)));
157159
};
158160
}

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

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.mock.web.MockHttpServletResponse;
2222
import org.springframework.test.web.servlet.StubMvcResult;
2323

24+
import static org.assertj.core.api.Assertions.assertThatCode;
2425
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2526
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
2627
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrlPattern;
@@ -39,99 +40,112 @@ public class MockMvcResultMatchersTests {
3940

4041
@Test
4142
public void redirect() throws Exception {
42-
redirectedUrl("/resource/1").match(getRedirectedUrlStubMvcResult("/resource/1"));
43+
assertThatCode(() -> redirectedUrl("/resource/1").match(redirectedUrlStub("/resource/1")))
44+
.doesNotThrowAnyException();
4345
}
4446

4547
@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>");
4952
}
5053

5154
@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>");
5559
}
5660

5761
@Test
5862
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();
6065
}
6166

6267
@Test
6368
public void redirectWithMatchingPattern() throws Exception {
64-
redirectedUrlPattern("/resource/*").match(getRedirectedUrlStubMvcResult("/resource/1"));
69+
assertThatCode(() -> redirectedUrlPattern("/resource/*").match(redirectedUrlStub("/resource/1")))
70+
.doesNotThrowAnyException();
6571
}
6672

6773
@Test
6874
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");
7178
}
7279

7380
@Test
7481
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/*'");
7785
}
7886

7987
@Test
8088
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();
8291
}
8392

8493
@Test
8594
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");
8898
}
8999

90100
@Test
91101
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/*'");
94105
}
95106

96107
@Test
97108
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();
99111
}
100112

101113
@Test
102114
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();
104117
}
105118

106119
@Test
107120
public void forwardWithMatchingPattern() throws Exception {
108-
forwardedUrlPattern("/api/**/?").match(getForwardedUrlStubMvcResult("/api/resource/1"));
121+
assertThatCode(() -> forwardedUrlPattern("/api/**/?").match(forwardedUrlStub("/api/resource/1")))
122+
.doesNotThrowAnyException();
109123
}
110124

111125
@Test
112126
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");
115130
}
116131

117132
@Test
118133
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/*'");
121137
}
122138

123-
private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {
139+
private StubMvcResult redirectedUrlStub(String redirectUrl) throws Exception {
124140
MockHttpServletResponse response = new MockHttpServletResponse();
125141
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);
128143
}
129144

130-
private StubMvcResult getForwardedUrlStubMvcResult(String forwardedUrl) {
145+
private StubMvcResult forwardedUrlStub(String forwardedUrl) {
131146
MockHttpServletResponse response = new MockHttpServletResponse();
132147
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);
135149
}
136150

137151
}

0 commit comments

Comments
 (0)