From c5cfe54c80ac1818dc015f53559c49a39ab11a43 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 1 Nov 2013 14:38:48 +0000 Subject: [PATCH] Add spring-boot tests to test starter --- .../spring-boot-sample-simple/pom.xml | 5 +++ ...pringTestSampleSimpleApplicationTests.java | 38 +++++++++++++++++++ .../spring-boot-starter-test/pom.xml | 6 +++ .../test/SpringApplicationContextLoader.java | 20 +++++++--- 4 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 spring-boot-samples/spring-boot-sample-simple/src/test/java/org/springframework/boot/sample/simple/SpringTestSampleSimpleApplicationTests.java diff --git a/spring-boot-samples/spring-boot-sample-simple/pom.xml b/spring-boot-samples/spring-boot-sample-simple/pom.xml index e51ecef13d6..f98e3169af5 100644 --- a/spring-boot-samples/spring-boot-sample-simple/pom.xml +++ b/spring-boot-samples/spring-boot-sample-simple/pom.xml @@ -18,6 +18,11 @@ ${project.groupId} spring-boot-starter + + ${project.groupId} + spring-boot-starter-test + test + diff --git a/spring-boot-samples/spring-boot-sample-simple/src/test/java/org/springframework/boot/sample/simple/SpringTestSampleSimpleApplicationTests.java b/spring-boot-samples/spring-boot-sample-simple/src/test/java/org/springframework/boot/sample/simple/SpringTestSampleSimpleApplicationTests.java new file mode 100644 index 00000000000..28878f458af --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-simple/src/test/java/org/springframework/boot/sample/simple/SpringTestSampleSimpleApplicationTests.java @@ -0,0 +1,38 @@ +/* + * 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.sample.simple; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationContextLoader; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Tests for {@link SampleSimpleApplication}. + * + * @author Dave Syer + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SampleSimpleApplication.class, loader=SpringApplicationContextLoader.class) +public class SpringTestSampleSimpleApplicationTests { + + @Test + public void testContextLoads() throws Exception { + } + +} diff --git a/spring-boot-starters/spring-boot-starter-test/pom.xml b/spring-boot-starters/spring-boot-starter-test/pom.xml index a473f690a6f..14354d0ddfb 100644 --- a/spring-boot-starters/spring-boot-starter-test/pom.xml +++ b/spring-boot-starters/spring-boot-starter-test/pom.xml @@ -18,6 +18,12 @@ spring-boot-starter-logging ${project.version} + + ${project.groupId} + spring-boot + tests + ${project.version} + junit junit diff --git a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoader.java b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoader.java index 1712724fa03..31ae078430c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoader.java +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoader.java @@ -80,12 +80,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { initializers.add(BeanUtils.instantiate(type)); } if (mergedConfig instanceof WebMergedContextConfiguration) { - WebMergedContextConfiguration webConfig = (WebMergedContextConfiguration) mergedConfig; - MockServletContext servletContext = new MockServletContext( - webConfig.getResourceBasePath()); - initializers.add(0, new ServletContextApplicationContextInitializer( - servletContext)); - application.setApplicationContextClass(GenericWebApplicationContext.class); + new WebConfigurer().setup(mergedConfig, application, initializers); } else { application.setWebEnvironment(false); @@ -105,4 +100,17 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { return "-context.xml"; } + private static class WebConfigurer { + void setup(MergedContextConfiguration mergedConfig, + SpringApplication application, + List> initializers) { + WebMergedContextConfiguration webConfig = (WebMergedContextConfiguration) mergedConfig; + MockServletContext servletContext = new MockServletContext( + webConfig.getResourceBasePath()); + initializers.add(0, new ServletContextApplicationContextInitializer( + servletContext)); + application.setApplicationContextClass(GenericWebApplicationContext.class); + } + } + }