001/*
002 * Copyright 2002-2017 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.setup;
018
019import org.springframework.lang.Nullable;
020import org.springframework.test.web.servlet.request.RequestPostProcessor;
021import org.springframework.web.context.WebApplicationContext;
022
023/**
024 * Contract for customizing a {@code ConfigurableMockMvcBuilder} in some
025 * specific way, e.g. a 3rd party library that wants to provide shortcuts for
026 * setting up a MockMvc.
027 *
028 * <p>An implementation of this interface can be plugged in via
029 * {@link ConfigurableMockMvcBuilder#apply} with instances of this type likely
030 * created via static methods, e.g.:
031 *
032 * <pre class="code">
033 * import static org.example.ExampleSetup.mySetup;
034 *
035 * // ...
036 *
037 * MockMvcBuilders.webAppContextSetup(context).apply(mySetup("foo","bar")).build();
038 * </pre>
039 *
040 * @author Rossen Stoyanchev
041 * @since 4.1
042 * @see org.springframework.test.web.servlet.setup.MockMvcConfigurerAdapter
043 */
044public interface MockMvcConfigurer {
045
046        /**
047         * Invoked immediately when this {@code MockMvcConfigurer} is added via
048         * {@link ConfigurableMockMvcBuilder#apply}.
049         * @param builder the builder for the MockMvc
050         */
051        default void afterConfigurerAdded(ConfigurableMockMvcBuilder<?> builder) {
052        }
053
054        /**
055         * Invoked when the MockMvc instance is about to be created with the MockMvc
056         * builder and the Spring WebApplicationContext that will be passed to the
057         * {@code DispatcherServlet}.
058         * @param builder the builder for the MockMvc
059         * @param context the Spring configuration
060         * @return a post processor to be applied to every request performed
061         * through the {@code MockMvc} instance.
062         */
063        @Nullable
064        default RequestPostProcessor beforeMockMvcCreated(
065                        ConfigurableMockMvcBuilder<?> builder, WebApplicationContext context) {
066
067                return null;
068        }
069
070}