ResulatMatcher.matchAll

Issue: SPR-16417
This commit is contained in:
Rossen Stoyanchev 2018-07-20 23:02:47 -04:00
parent 91122ec0f5
commit c57e1afd2b
2 changed files with 30 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,14 +40,21 @@ public interface ResultActions {
* .andExpect(status().isOk()) * .andExpect(status().isOk())
* .andExpect(content().contentType(MediaType.APPLICATION_JSON)) * .andExpect(content().contentType(MediaType.APPLICATION_JSON))
* .andExpect(jsonPath("$.person.name").value("Jason")); * .andExpect(jsonPath("$.person.name").value("Jason"));
* </pre>
*
* <p>Or alternatively provide all matchers as a vararg:
* <pre class="code">
* static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAll
* *
* mockMvc.perform(post("/form")) * mockMvc.perform(post("/form"))
* .andExpect(status().isOk()) * .andExpect(matchAll(
* .andExpect(redirectedUrl("/person/1")) * status().isOk(),
* .andExpect(model().size(1)) * redirectedUrl("/person/1"),
* .andExpect(model().attributeExists("person")) * model().size(1),
* .andExpect(flash().attributeCount(1)) * model().attributeExists("person"),
* .andExpect(flash().attribute("message", "success!")); * flash().attributeCount(1),
* flash().attribute("message", "success!"))
* );
* </pre> * </pre>
*/ */
ResultActions andExpect(ResultMatcher matcher) throws Exception; ResultActions andExpect(ResultMatcher matcher) throws Exception;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -49,12 +49,26 @@ package org.springframework.test.web.servlet;
@FunctionalInterface @FunctionalInterface
public interface ResultMatcher { public interface ResultMatcher {
/** /**
* Assert the result of an executed request. * Assert the result of an executed request.
*
* @param result the result of the executed request * @param result the result of the executed request
* @throws Exception if a failure occurs * @throws Exception if a failure occurs
*/ */
void match(MvcResult result) throws Exception; void match(MvcResult result) throws Exception;
/**
* Static method for matching with an array of result matchers.
* @param matchers the matchers
* @since 5.1
*/
static ResultMatcher matchAll(ResultMatcher... matchers) {
return result -> {
for (ResultMatcher matcher : matchers) {
matcher.match(result);
}
};
}
} }