Given a Spring Boot + Thymeleaf web application (this is nearly identical to the Spring project's gs-consuming-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
...the user is greeted just fine with "Hello World!" at http://localhost:8080/
, but the wiring of Spring's "context" does not seem to apply within the integration test ( ControllerTest.java
):
java.lang.AssertionError: Status
Expected :200
Actual :404
What is wrong with the project layout , and/or the configuration annotations in the test?
src/main/webapp/
is intentionally missing, along with things like web.xml
and WEB-INF/
. The goal here is to use minimal configuration, with an integration test to test-drive the development of the view and controller of the application.
Gory details below. Sorry in advance for the "wall of text."
<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>
package hello;
// ...
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
package hello;
@org.springframework.stereotype.Controller
public class Controller {
}
package hello;
// ...
@Configuration
public class Config extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
}
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());
}
}
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<p>Hello world!</p>
</body>
</html>
Thanks to @M.Deinum for helping me to realize 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 suppose that @ContextConfiguration
is meant for integration tests in Spring , whereas @SpringApplicationConfiguration
is meant for integration tests in Spring Boot .
According to the Javadoc for the latter :
Class-level annotation that is used to determine how to load and configure an ApplicationContext for integration tests.
Similar to the standard @ContextConfiguration but uses Spring Boot's SpringApplicationContextLoader.
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");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.