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 index 04e6f981758..77cac6dba47 100644 --- a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationIntegrationTestTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -16,9 +16,12 @@ package org.springframework.boot.test; +import javax.servlet.ServletContext; + import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; @@ -31,11 +34,14 @@ 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.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertSame; /** * Tests for {@link IntegrationTest} @@ -54,6 +60,12 @@ public class SpringApplicationIntegrationTestTests { @Value("${value}") private int value = 0; + @Autowired + private WebApplicationContext context; + + @Autowired + private ServletContext servletContext; + @Test public void runAndTestHttpEndpoint() { assertNotEquals(8080, this.port); @@ -68,6 +80,12 @@ public class SpringApplicationIntegrationTestTests { assertEquals(123, this.value); } + @Test + public void validateWebApplicationContextIsSet() { + assertSame(this.context, WebApplicationContextUtils + .getWebApplicationContext(this.servletContext)); + } + @Configuration @EnableWebMvc @RestController diff --git a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationMockMvcTests.java b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationMockMvcTests.java new file mode 100644 index 00000000000..141229c63f8 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationMockMvcTests.java @@ -0,0 +1,91 @@ +/* + * Copyright 2012-2015 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 javax.servlet.ServletContext; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationMockMvcTests.Config; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +import static org.junit.Assert.assertSame; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * Tests for {@link WebAppConfiguration} integration. + * + * @author Stephane Nicoll + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Config.class) +@WebAppConfiguration +public class SpringApplicationMockMvcTests { + + @Autowired + private WebApplicationContext context; + + @Autowired + private ServletContext servletContext; + + private MockMvc mvc; + + @Before + public void setUp() { + this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build(); + } + + @Test + public void testMockHttpEndpoint() throws Exception { + this.mvc.perform(get("/")).andExpect(status().isOk()) + .andExpect(content().string("Hello World")); + } + + @Test + public void validateWebApplicationContextIsSet() { + assertSame(this.context, WebApplicationContextUtils + .getWebApplicationContext(this.servletContext)); + } + + + @Configuration + @EnableWebMvc + @RestController + protected static class Config { + + @RequestMapping("/") + public String home() { + return "Hello World"; + } + + } + +} diff --git a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationWebIntegrationTestTests.java b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationWebIntegrationTestTests.java index 59f3543bab8..964f73ea2b5 100644 --- a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationWebIntegrationTestTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationWebIntegrationTestTests.java @@ -16,9 +16,12 @@ package org.springframework.boot.test; +import javax.servlet.ServletContext; + import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; @@ -30,11 +33,14 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertSame; /** * Tests for {@link IntegrationTest} @@ -43,7 +49,7 @@ import static org.junit.Assert.assertNotEquals; */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(Config.class) -@WebIntegrationTest({ "server.port=0", "value=123" }) +@WebIntegrationTest({"server.port=0", "value=123"}) public class SpringApplicationWebIntegrationTestTests { @Value("${local.server.port}") @@ -52,6 +58,12 @@ public class SpringApplicationWebIntegrationTestTests { @Value("${value}") private int value = 0; + @Autowired + private WebApplicationContext context; + + @Autowired + private ServletContext servletContext; + @Test public void runAndTestHttpEndpoint() { assertNotEquals(8080, this.port); @@ -66,6 +78,12 @@ public class SpringApplicationWebIntegrationTestTests { assertEquals(123, this.value); } + @Test + public void validateWebApplicationContextIsSet() { + assertSame(this.context, WebApplicationContextUtils + .getWebApplicationContext(this.servletContext)); + } + @Configuration @EnableWebMvc @RestController