001/* 002 * Copyright 2002-2018 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.test.web.servlet.MockMvc; 020import org.springframework.test.web.servlet.MockMvcBuilder; 021import org.springframework.web.context.WebApplicationContext; 022 023/** 024 * The main class to import in order to access all available {@link MockMvcBuilder}s. 025 * 026 * <h3>Eclipse Users</h3> 027 * <p>Consider adding this class as a Java editor favorite. To navigate to 028 * this setting, open the Preferences and type "favorites". 029 * 030 * @author Rossen Stoyanchev 031 * @author Sam Brannen 032 * @since 3.2 033 * @see #webAppContextSetup(WebApplicationContext) 034 * @see #standaloneSetup(Object...) 035 */ 036public class MockMvcBuilders { 037 038 /** 039 * Build a {@link MockMvc} instance using the given, fully initialized 040 * (i.e., <em>refreshed</em>) {@link WebApplicationContext}. 041 * <p>The {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} 042 * will use the context to discover Spring MVC infrastructure and application 043 * controllers in it. The context must have been configured with a 044 * {@link javax.servlet.ServletContext ServletContext}. 045 */ 046 public static DefaultMockMvcBuilder webAppContextSetup(WebApplicationContext context) { 047 return new DefaultMockMvcBuilder(context); 048 } 049 050 /** 051 * Build a {@link MockMvc} instance by registering one or more 052 * {@code @Controller} instances and configuring Spring MVC infrastructure 053 * programmatically. 054 * <p>This allows full control over the instantiation and initialization of 055 * controllers and their dependencies, similar to plain unit tests while 056 * also making it possible to test one controller at a time. 057 * <p>When this builder is used, the minimum infrastructure required by the 058 * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} 059 * to serve requests with annotated controllers is created automatically 060 * and can be customized, resulting in configuration that is equivalent to 061 * what MVC Java configuration provides except using builder-style methods. 062 * <p>If the Spring MVC configuration of an application is relatively 063 * straight-forward — for example, when using the MVC namespace in 064 * XML or MVC Java config — then using this builder might be a good 065 * option for testing a majority of controllers. In such cases, a much 066 * smaller number of tests can be used to focus on testing and verifying 067 * the actual Spring MVC configuration. 068 * @param controllers one or more {@code @Controller} instances to test 069 */ 070 public static StandaloneMockMvcBuilder standaloneSetup(Object... controllers) { 071 return new StandaloneMockMvcBuilder(controllers); 072 } 073 074}