From 123457a58850be6b36e09db6195c42a462e1bb8d Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 11 Mar 2014 12:36:03 +0000 Subject: [PATCH] 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 --- .../boot/test/IntegrationTest.java | 48 ++++++++++++ .../test/SpringApplicationContextLoader.java | 22 ++++-- ...SpringApplicationIntegrationTestTests.java | 76 +++++++++++++++++++ 3 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/test/IntegrationTest.java create mode 100644 spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java diff --git a/spring-boot/src/main/java/org/springframework/boot/test/IntegrationTest.java b/spring-boot/src/main/java/org/springframework/boot/test/IntegrationTest.java new file mode 100644 index 0000000000..43ef3fd539 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/test/IntegrationTest.java @@ -0,0 +1,48 @@ +/* + * 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 { + +} diff --git a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java index e2d155e896..6b1c906895 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java @@ -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 getArgs(MergedContextConfiguration mergedConfig) { Map args = new LinkedHashMap(); - // 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> 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); + } } } diff --git a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java new file mode 100644 index 0000000000..688267732c --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java @@ -0,0 +1,76 @@ +/* + * 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"; + } + + } + +}