提交 1d35c7c5 编写于 作者: R Rossen Stoyanchev

Polish MockMvc setup classes

上级 2d951994
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
......@@ -33,14 +33,16 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.test.web.servlet.MockMvcBuilder;
/**
* An abstract implementation of {@link org.springframework.test.web.servlet.MockMvcBuilder}
* with common methods for configuring filters, default request properties, global
* expectations and global result actions.
* <p>
* Sub-classes can use different strategies to prepare a WebApplicationContext to
* pass to the DispatcherServlet.
* Abstract implementation of {@link MockMvcBuilder} with common methods for
* configuring filters, default request properties, global expectations and
* global result actions.
*
* <p>Sub-classes can use different strategies to prepare the Spring
* {@code WebApplicationContext} that will be passed to the
* {@code DispatcherServlet}.
*
* @author Rossen Stoyanchev
* @author Stephane Nicoll
......@@ -64,7 +66,6 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
public final <T extends B> T addFilters(Filter... filters) {
Assert.notNull(filters, "filters cannot be null");
for (Filter f : filters) {
Assert.notNull(f, "filters cannot contain null values");
this.filters.add(f);
......@@ -73,14 +74,11 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
}
public final <T extends B> T addFilter(Filter filter, String... urlPatterns) {
Assert.notNull(filter, "filter cannot be null");
Assert.notNull(urlPatterns, "urlPatterns cannot be null");
if (urlPatterns.length > 0) {
filter = new PatternMappingFilterProxy(filter, urlPatterns);
}
this.filters.add(filter);
return self();
}
......@@ -153,9 +151,9 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
}
/**
* A method to obtain the WebApplicationContext to be passed to the DispatcherServlet.
* Invoked from {@link #build()} before the
* {@link org.springframework.test.web.servlet.MockMvc} instance is created.
* A method to obtain the {@code WebApplicationContext} to be passed to the
* {@code DispatcherServlet}. Invoked from {@link #build()} before the
* {@link MockMvc} instance is created.
*/
protected abstract WebApplicationContext initWebAppContext();
......
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2017 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.
......@@ -29,7 +29,8 @@ import org.springframework.test.web.servlet.ResultMatcher;
* @author Rossen Stoyanchev
* @since 4.1
*/
public interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder<B>> extends MockMvcBuilder {
public interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder<B>>
extends MockMvcBuilder {
/**
* Add filters mapped to any request (i.e. "/*"). For example:
......
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2017 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.
......@@ -19,17 +19,21 @@ package org.springframework.test.web.servlet.setup;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
import org.springframework.web.context.WebApplicationContext;
/**
* Allows a sub-class to encapsulate logic for pre-configuring a
* {@code ConfigurableMockMvcBuilder} for some specific purpose. A 3rd party
* library may use this to provide shortcuts for setting up MockMvc.
* Contract for customizing a {@code ConfigurableMockMvcBuilder} in some
* specific way, e.g. a 3rd party library that wants to provide shortcuts for
* setting up a MockMvc.
*
* <p>Can be plugged in via {@link ConfigurableMockMvcBuilder#apply} with
* instances of this type likely created via static methods, e.g.:
* <p>An implementation of this interface can be plugged in via
* {@link ConfigurableMockMvcBuilder#apply} with instances of this type likely
* created via static methods, e.g.:
*
* <pre class="code">
* MockMvcBuilders.webAppContextSetup(context).apply(mySetup("foo","bar")).build();
* import static org.example.ExampleSetup.mySetup;
*
* // ...
*
* MockMvcBuilders.webAppContextSetup(context).apply(mySetup("foo","bar")).build();
* </pre>
*
* @author Rossen Stoyanchev
......@@ -39,16 +43,22 @@ import org.springframework.web.context.WebApplicationContext;
public interface MockMvcConfigurer {
/**
* Invoked immediately after a {@code MockMvcConfigurer} is added via
* Invoked immediately when this {@code MockMvcConfigurer} is added via
* {@link ConfigurableMockMvcBuilder#apply}.
* @param builder the builder for the MockMvc
*/
void afterConfigurerAdded(ConfigurableMockMvcBuilder<?> builder);
/**
* Invoked just before the MockMvc instance is created. Implementations may
* return a RequestPostProcessor to be applied to every request performed
* through the created {@code MockMvc} instance.
* Invoked when the MockMvc instance is about to be created with the MockMvc
* builder and the Spring WebApplicationContext that will be passed to the
* {@code DispatcherServlet}.
* @param builder the builder for the MockMvc
* @param context the Spring configuration
* @return a post processor to be applied to every request performed
* through the {@code MockMvc} instance.
*/
RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder<?> builder, WebApplicationContext context);
RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder<?> builder,
WebApplicationContext context);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册