Skip to content

Commit c57e1af

Browse files
committed
ResulatMatcher.matchAll
Issue: SPR-16417
1 parent 91122ec commit c57e1af

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

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

Lines changed: 14 additions & 7 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-2018 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.
@@ -40,14 +40,21 @@ public interface ResultActions {
4040
* .andExpect(status().isOk())
4141
* .andExpect(content().contentType(MediaType.APPLICATION_JSON))
4242
* .andExpect(jsonPath("$.person.name").value("Jason"));
43+
* </pre>
44+
*
45+
* <p>Or alternatively provide all matchers as a vararg:
46+
* <pre class="code">
47+
* static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAll
4348
*
4449
* mockMvc.perform(post("/form"))
45-
* .andExpect(status().isOk())
46-
* .andExpect(redirectedUrl("/person/1"))
47-
* .andExpect(model().size(1))
48-
* .andExpect(model().attributeExists("person"))
49-
* .andExpect(flash().attributeCount(1))
50-
* .andExpect(flash().attribute("message", "success!"));
50+
* .andExpect(matchAll(
51+
* status().isOk(),
52+
* redirectedUrl("/person/1"),
53+
* model().size(1),
54+
* model().attributeExists("person"),
55+
* flash().attributeCount(1),
56+
* flash().attribute("message", "success!"))
57+
* );
5158
* </pre>
5259
*/
5360
ResultActions andExpect(ResultMatcher matcher) throws Exception;

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -49,12 +49,26 @@
4949
@FunctionalInterface
5050
public interface ResultMatcher {
5151

52+
5253
/**
5354
* Assert the result of an executed request.
54-
*
5555
* @param result the result of the executed request
5656
* @throws Exception if a failure occurs
5757
*/
5858
void match(MvcResult result) throws Exception;
5959

60+
61+
/**
62+
* Static method for matching with an array of result matchers.
63+
* @param matchers the matchers
64+
* @since 5.1
65+
*/
66+
static ResultMatcher matchAll(ResultMatcher... matchers) {
67+
return result -> {
68+
for (ResultMatcher matcher : matchers) {
69+
matcher.match(result);
70+
}
71+
};
72+
}
73+
6074
}

0 commit comments

Comments
 (0)