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 MockMvcBuilders}. 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 final class MockMvcBuilders { 037 038 private MockMvcBuilders() { 039 } 040 041 042 /** 043 * Build a {@link MockMvc} instance using the given, fully initialized 044 * (i.e., <em>refreshed</em>) {@link WebApplicationContext}. 045 * <p>The {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} 046 * will use the context to discover Spring MVC infrastructure and application 047 * controllers in it. The context must have been configured with a 048 * {@link javax.servlet.ServletContext ServletContext}. 049 */ 050 public static DefaultMockMvcBuilder webAppContextSetup(WebApplicationContext context) { 051 return new DefaultMockMvcBuilder(context); 052 } 053 054 /** 055 * Build a {@link MockMvc} instance by registering one or more 056 * {@code @Controller} instances and configuring Spring MVC infrastructure 057 * programmatically. 058 * <p>This allows full control over the instantiation and initialization of 059 * controllers and their dependencies, similar to plain unit tests while 060 * also making it possible to test one controller at a time. 061 * <p>When this builder is used, the minimum infrastructure required by the 062 * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} 063 * to serve requests with annotated controllers is created automatically 064 * and can be customized, resulting in configuration that is equivalent to 065 * what MVC Java configuration provides except using builder-style methods. 066 * <p>If the Spring MVC configuration of an application is relatively 067 * straight-forward — for example, when using the MVC namespace in 068 * XML or MVC Java config — then using this builder might be a good 069 * option for testing a majority of controllers. In such cases, a much 070 * smaller number of tests can be used to focus on testing and verifying 071 * the actual Spring MVC configuration. 072 * @param controllers one or more {@code @Controller} instances to test 073 * (specified {@code Class} will be turned into instance) 074 */ 075 public static StandaloneMockMvcBuilder standaloneSetup(Object... controllers) { 076 return new StandaloneMockMvcBuilder(controllers); 077 } 078 079}