@WebAppConfiguration and @ContextConfiguration for Spring Boot + Thymeleaf

Given the Spring Boot + Thymeleaf web application (this is almost identical to the Spring gs project -consumption-rest "initial" code tree ):

β”œβ”€β”€ pom.xml └── src β”œβ”€β”€ main β”‚  β”œβ”€β”€ java β”‚  β”‚  └── hello β”‚  β”‚  β”œβ”€β”€ Application.java β”‚  β”‚  β”œβ”€β”€ Config.java β”‚  β”‚  └── Controller.java β”‚  └── resources β”‚  └── templates β”‚  └── index.html └── test └── java └── hello └── ControllerTest.java 

... user is welcomed just fine with Hello World! at http://localhost:8080/ , but Spring's β€œcontext” posting doesn't seem to apply in the integration test ( ControllerTest.java ):

 java.lang.AssertionError: Status Expected :200 Actual :404 

What happened to the project layout and / or configuration annotations in the test?

src/main/webapp/ intentionally missing along with things like web.xml and WEB-INF/ . The goal here is to use a minimal configuration with an integration test to test the development of the view and application controller.

More details. Sorry for the "text wall" in advance.


pom.xml

 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.1.RELEASE</version> </parent> <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> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> 

Application.java

 package hello; // ... @SpringBootApplication public class Application { public static void main(String[] args) throws Throwable { SpringApplication.run(Application.class, args); } } 

Controller.java

 package hello; @org.springframework.stereotype.Controller public class Controller { } 

Config.java

 package hello; // ... @Configuration public class Config extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); } } 

ControllerTest.java

 package hello; // ... @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = Config.class) public class ControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void test() throws Exception { this.mockMvc .perform(get("/")) .andExpect(status().isOk()); } } 

index.html

 <!DOCTYPE html> <html> <head> <title>Hello World!</title> </head> <body> <p>Hello world!</p> </body> </html> 
+6
source share
2 answers

Thanks @ M.Deinum for helping me understand that:

 @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = Config.class) public class ControllerTest { 

... should be:

 @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @SpringApplicationConfiguration(classes = Application.class) public class ControllerTest { 

I believe that @ContextConfiguration is for integration tests in Spring , while @SpringApplicationConfiguration is for integration tests in Spring Boot .

According to Javadoc for the latter :

Class level annotations that are used to determine how to load and configure ApplicationContext for integration tests.

Like the @ContextConfiguration standard, but uses Spring Boot SpringApplicationContextLoader.

+10
source
  package com.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import netgloo.Application; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) public class SmokeTest { @Test public void contexLoads() throws Exception { System.out.println("Test"); } } 
0
source

All Articles