001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.web.servlet;
018
019/**
020 * A {@code ResultMatcher} matches the result of an executed request against
021 * some expectation.
022 *
023 * <p>See static factory methods in
024 * {@link org.springframework.test.web.servlet.result.MockMvcResultMatchers
025 * MockMvcResultMatchers}.
026 *
027 * <h3>Example Using Status and Content Result Matchers</h3>
028 *
029 * <pre class="code">
030 * import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
031 * import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
032 * import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
033 *
034 * // ...
035 *
036 * WebApplicationContext wac = ...;
037 *
038 * MockMvc mockMvc = webAppContextSetup(wac).build();
039 *
040 * mockMvc.perform(get("/form"))
041 *   .andExpect(status().isOk())
042 *   .andExpect(content().mimeType(MediaType.APPLICATION_JSON));
043 * </pre>
044 *
045 * @author Rossen Stoyanchev
046 * @author Sam Brannen
047 * @since 3.2
048 */
049@FunctionalInterface
050public interface ResultMatcher {
051
052        /**
053         * Assert the result of an executed request.
054         * @param result the result of the executed request
055         * @throws Exception if a failure occurs
056         */
057        void match(MvcResult result) throws Exception;
058
059
060        /**
061         * Static method for matching with an array of result matchers.
062         * @param matchers the matchers
063         * @since 5.1
064         */
065        static ResultMatcher matchAll(ResultMatcher... matchers) {
066                return result -> {
067                        for (ResultMatcher matcher : matchers) {
068                                matcher.match(result);
069                        }
070                };
071        }
072
073}