Rework SpringApplicationTest to support web modes
Rework the new testing support so that @SpringApplicationTest can be used for standard integration tests, web integration tests with a mock Servlet environment and web integration tests with an embedded servlet container. This means that it a replacement for 1.3's @IntegrationTest and @WebIntegrationTest and allows all SpringApplication testing to be configured using a common annotation. The old @IntegrationTest and @WebIntegrationTest along with their supporting classes have been reinstated to their previous form (while remaining deprecated). This should ensure that they continue to work in 1.4 exactly as they did in 1.3 giving users a smooth path to @SpringApplicationTest. See gh-5477pull/5552/head
parent
893a6c32f3
commit
33f0ea3480
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContextBootstrapper;
|
||||
import org.springframework.test.context.support.DefaultTestContextBootstrapper;
|
||||
import org.springframework.test.context.web.WebDelegatingSmartContextLoader;
|
||||
import org.springframework.test.context.web.WebMergedContextConfiguration;
|
||||
|
||||
/**
|
||||
* {@link TestContextBootstrapper} for Spring Boot web integration tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.2.1
|
||||
* @deprecated Since 1.4.0
|
||||
*/
|
||||
@Deprecated
|
||||
class WebAppIntegrationTestContextBootstrapper extends DefaultTestContextBootstrapper {
|
||||
|
||||
@Override
|
||||
protected Class<? extends ContextLoader> getDefaultContextLoaderClass(
|
||||
Class<?> testClass) {
|
||||
if (AnnotationUtils.findAnnotation(testClass, WebIntegrationTest.class) != null) {
|
||||
return WebDelegatingSmartContextLoader.class;
|
||||
}
|
||||
return super.getDefaultContextLoaderClass(testClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MergedContextConfiguration processMergedContextConfiguration(
|
||||
MergedContextConfiguration mergedConfig) {
|
||||
WebIntegrationTest annotation = AnnotationUtils
|
||||
.findAnnotation(mergedConfig.getTestClass(), WebIntegrationTest.class);
|
||||
if (annotation != null) {
|
||||
mergedConfig = new WebMergedContextConfiguration(mergedConfig, null);
|
||||
MergedContextConfigurationProperties properties = new MergedContextConfigurationProperties(
|
||||
mergedConfig);
|
||||
if (annotation.randomPort()) {
|
||||
properties.add(annotation.value(), "server.port:0");
|
||||
}
|
||||
else {
|
||||
properties.add(annotation.value());
|
||||
}
|
||||
}
|
||||
return mergedConfig;
|
||||
}
|
||||
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.BootstrapWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
/**
|
||||
* Test class annotation signifying that the tests are "integration tests" for a
|
||||
* {@link org.springframework.boot.SpringApplication Spring Boot Application}. By default
|
||||
* will load nested {@code @Configuration} classes, or fallback an
|
||||
* {@link SpringApplicationConfiguration @SpringApplicationConfiguration} search. Unless
|
||||
* otherwise configured, a {@link SpringApplicationContextLoader} will be used to load the
|
||||
* {@link ApplicationContext}. Use
|
||||
* {@link SpringApplicationConfiguration @SpringApplicationConfiguration} or
|
||||
* {@link ContextConfiguration @ContextConfiguration} if custom configuration is required.
|
||||
* <p>
|
||||
* It's recommended that {@code @IntegrationTest} is used only for non-web applications
|
||||
* (i.e. not combined with {@link WebAppConfiguration @WebAppConfiguration}). If you want
|
||||
* to start a real embedded servlet container in the same way as a production application
|
||||
* (listening on normal ports) use
|
||||
* {@link org.springframework.boot.test.context.web.WebIntegrationTest @WebIntegrationTest}
|
||||
* instead. If you are testing a web application and want to mock the servlet environment
|
||||
* (for example so that you can use {@link MockMvc}) you should switch to the
|
||||
* {@link SpringApplicationTest @SpringApplicationTest} annotation.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
* @see SpringApplicationTest
|
||||
* @see org.springframework.boot.test.context.web.WebIntegrationTest
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@BootstrapWith(IntegrationTestContextBootstrapper.class)
|
||||
public @interface IntegrationTest {
|
||||
|
||||
/**
|
||||
* Properties in form {@literal key=value} that should be added to the Spring
|
||||
* {@link Environment} before the test runs.
|
||||
* @return the environment properties
|
||||
*/
|
||||
String[] value() default {};
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue