Tom*_*ton 8 java spring integration-testing thymeleaf spring-boot
给定一个Spring Boot + Thymeleaf Web应用程序(这与Spring项目的gs- consume -rest"初始"代码树几乎相同):
??? pom.xml
??? src
??? main
? ??? java
? ? ??? hello
? ? ??? Application.java
? ? ??? Config.java
? ? ??? Controller.java
? ??? resources
? ??? templates
? ??? index.html
??? test
??? java
??? hello
??? ControllerTest.java
Run Code Online (Sandbox Code Playgroud)
......"Hello World!"让用户受到了满意的欢迎!在http://localhost:8080/,但Spring的"上下文"的连接似乎不适用于集成测试(ControllerTest.java):
java.lang.AssertionError: Status
Expected :200
Actual :404
Run Code Online (Sandbox Code Playgroud)
有什么不对的项目布局,和/或配置的注释在测试?
src/main/webapp/是故意失踪,有喜欢的东西一起web.xml和WEB-INF/.这里的目标是使用最小配置,通过集成测试来测试驱动应用程序的视图和控制器的开发.
血腥细节如下.提前抱歉"文字墙".
<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>
Run Code Online (Sandbox Code Playgroud)
package hello;
// ...
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
package hello;
@org.springframework.stereotype.Controller
public class Controller {
}
Run Code Online (Sandbox Code Playgroud)
package hello;
// ...
@Configuration
public class Config extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
}
Run Code Online (Sandbox Code Playgroud)
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());
}
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<p>Hello world!</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Tom*_*ton 10
感谢@ M.Deinum让我意识到:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Config.class)
public class ControllerTest {
Run Code Online (Sandbox Code Playgroud)
...应该:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes = Application.class)
public class ControllerTest {
Run Code Online (Sandbox Code Playgroud)
我认为这@ContextConfiguration适用于Spring中的集成测试,而是@SpringApplicationConfiguration用于Spring Boot中的集成测试.
类级注释,用于确定如何为集成测试加载和配置ApplicationContext.
与标准@ContextConfiguration类似,但使用Spring Boot的SpringApplicationContextLoader.
| 归档时间: |
|
| 查看次数: |
17340 次 |
| 最近记录: |