Commit 123457a5 authored by Dave Syer's avatar Dave Syer

Add @IntegrationTest annotation for test classes

Allowing use of embededded container in @SpringApplicationConfiguration
tests, e.g.

```
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Config.class)
@WebAppConfiguration
@IntegrationTest
public class SpringApplicationIntegrationTestTests {

	@Test
	public void nestedConfigClasses() {
		String body = new RestTemplate().getForObject("http://localhost:8080/",
				String.class);
		assertEquals("Hello World", body);
	}

	@Configuration
	@EnableWebMvc
	@RestController
	protected static class Config {

		@Bean
		public DispatcherServlet dispatcherServlet() {
			return new DispatcherServlet();
		}

		@Bean
		public EmbeddedServletContainerFactory embeddedServletContainer() {
			return new TomcatEmbeddedServletContainerFactory();
		}

		@RequestMapping("/")
		public String home() {
			return "Hello World";
		}

	}

}
```

Fixes gh-473
parent 4f62a7c6
/*
* Copyright 2012-2013 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 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.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
/**
* Test class annotation signifying that the tests are integration tests (and therefore
* require an application to startup "fully leaded" and listening on their normal ports).
*
* @author Dave Syer
*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
// Leave out the ServletTestExecutionListener because it only deals with Mock* servlet
// stuff. A real embedded application will not need the mocks.
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class })
public @interface IntegrationTest {
}
......@@ -30,6 +30,7 @@ import org.springframework.boot.context.web.ServletContextApplicationContextInit
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.core.SpringVersion;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextLoader;
......@@ -127,8 +128,11 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
private Map<String, Object> getArgs(MergedContextConfiguration mergedConfig) {
Map<String, Object> args = new LinkedHashMap<String, Object>();
// Not running an embedded server, just setting up web context
args.put("server.port", "-1");
if (AnnotationUtils.findAnnotation(mergedConfig.getTestClass(),
IntegrationTest.class) == null) {
// Not running an embedded server, just setting up web context
args.put("server.port", "-1");
}
// JMX bean names will clash if the same bean is used in multiple contexts
args.put("spring.jmx.enabled", "false");
return args;
......@@ -161,11 +165,15 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
SpringApplication application,
List<ApplicationContextInitializer<?>> initializers) {
WebMergedContextConfiguration webConfig = (WebMergedContextConfiguration) mergedConfig;
MockServletContext servletContext = new MockServletContext(
webConfig.getResourceBasePath());
initializers.add(0, new ServletContextApplicationContextInitializer(
servletContext));
application.setApplicationContextClass(GenericWebApplicationContext.class);
if (AnnotationUtils.findAnnotation(webConfig.getTestClass(),
IntegrationTest.class) == null) {
MockServletContext servletContext = new MockServletContext(
webConfig.getResourceBasePath());
initializers.add(0, new ServletContextApplicationContextInitializer(
servletContext));
application
.setApplicationContextClass(GenericWebApplicationContext.class);
}
}
}
......
/*
* Copyright 2012-2014 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.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.test.SpringApplicationIntegrationTestTests.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.junit.Assert.assertEquals;
/**
* Tests for {@link IntegrationTest}
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Config.class)
@WebAppConfiguration
@IntegrationTest
public class SpringApplicationIntegrationTestTests {
@Test
public void nestedConfigClasses() {
String body = new RestTemplate().getForObject("http://localhost:8080/",
String.class);
assertEquals("Hello World", body);
}
@Configuration
@EnableWebMvc
@RestController
protected static class Config {
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Bean
public EmbeddedServletContainerFactory embeddedServletContainer() {
return new TomcatEmbeddedServletContainerFactory();
}
@RequestMapping("/")
public String home() {
return "Hello World";
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment