Browse Source

ResulatMatcher.matchAll

Issue: SPR-16417
pull/1651/merge
Rossen Stoyanchev 8 years ago
parent
commit
c57e1afd2b
  1. 21
      spring-test/src/main/java/org/springframework/test/web/servlet/ResultActions.java
  2. 18
      spring-test/src/main/java/org/springframework/test/web/servlet/ResultMatcher.java

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -40,14 +40,21 @@ public interface ResultActions { @@ -40,14 +40,21 @@ public interface ResultActions {
* .andExpect(status().isOk())
* .andExpect(content().contentType(MediaType.APPLICATION_JSON))
* .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"))
* .andExpect(status().isOk())
* .andExpect(redirectedUrl("/person/1"))
* .andExpect(model().size(1))
* .andExpect(model().attributeExists("person"))
* .andExpect(flash().attributeCount(1))
* .andExpect(flash().attribute("message", "success!"));
* .andExpect(matchAll(
* status().isOk(),
* redirectedUrl("/person/1"),
* model().size(1),
* model().attributeExists("person"),
* flash().attributeCount(1),
* flash().attribute("message", "success!"))
* );
* </pre>
*/
ResultActions andExpect(ResultMatcher matcher) throws Exception;

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -49,12 +49,26 @@ package org.springframework.test.web.servlet; @@ -49,12 +49,26 @@ package org.springframework.test.web.servlet;
@FunctionalInterface
public interface ResultMatcher {
/**
* Assert the result of an executed request.
*
* @param result the result of the executed request
* @throws Exception if a failure occurs
*/
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);
}
};
}
}

Loading…
Cancel
Save