This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
732447d
commit 321c4c3
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.test</groupId> | ||
<artifactId>demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>demo</name> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.2.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<!-- version 4.1.3 lets the test succeed, higher versions fail --> | ||
<spring.version>4.1.3.RELEASE</spring.version> | ||
<java.version>1.7</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
45 changes: 45 additions & 0 deletions
45
SPR-13075/src/test/java/demo/DemoStandaloneSetupTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package demo; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import javax.servlet.ServletContext; | ||
import javax.servlet.ServletRequest; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import org.springframework.stereotype.Controller; | ||
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.ResponseBody; | ||
import org.springframework.web.context.support.WebApplicationContextUtils; | ||
|
||
public class DemoStandaloneSetupTests { | ||
|
||
MockMvc mockMvc; | ||
|
||
@Before | ||
public void setUp() { | ||
mockMvc = MockMvcBuilders.standaloneSetup(new MyController()).build(); | ||
} | ||
|
||
@Test | ||
public void rootContext() throws Exception { | ||
mockMvc.perform(get("/test")).andExpect(content().string("true")); | ||
} | ||
|
||
|
||
@Controller | ||
public static class MyController { | ||
|
||
@RequestMapping("/test") | ||
@ResponseBody | ||
public boolean handle(ServletRequest request) { | ||
return WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()) != null; | ||
} | ||
|
||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
SPR-13075/src/test/java/demo/DemoWebApplicationContextSetupTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package demo; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.ContextHierarchy; | ||
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.context.WebApplicationContext; | ||
import org.springframework.web.context.support.WebApplicationContextUtils; | ||
|
||
import javax.servlet.ServletContext; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextHierarchy({ | ||
@ContextConfiguration(classes = DemoWebApplicationContextSetupTests.RootConfig.class), | ||
@ContextConfiguration(classes = DemoWebApplicationContextSetupTests.ServletConfig.class) | ||
}) | ||
@WebAppConfiguration | ||
public class DemoWebApplicationContextSetupTests { | ||
|
||
@Configuration | ||
static class RootConfig { | ||
@Bean | ||
public String fooBean() { | ||
return "foo"; | ||
} | ||
} | ||
|
||
@Configuration | ||
static class ServletConfig { } | ||
|
||
@Autowired WebApplicationContext applicationContext; | ||
@Autowired ServletContext servletContext; | ||
MockMvc mockMvc; | ||
|
||
@Before | ||
public void setUp() { | ||
// comment this out to see that the test will succeed then with Spring versions > 4.1.3 | ||
mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build(); | ||
} | ||
|
||
@Test | ||
public void rootContext() { | ||
WebApplicationContext rootContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); | ||
assertNull("Root context should not have parent context", rootContext.getParent()); | ||
assertNotNull("Root context should contain bean of type String", rootContext.getBeansOfType(String.class)); | ||
} | ||
|
||
} |