java Spring Boot + Thymeleaf 的 @WebAppConfiguration 和 @ContextConfiguration
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34606760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@WebAppConfiguration and @ContextConfiguration for Spring Boot + Thymeleaf
提问by Tommy Stanton
Given a Spring Boot + Thymeleaf web application (this is nearly identical to the Spring project's gs-consuming-rest "initial" code tree):
给定一个 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
...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
):
...用户会收到“Hello World!”的问候。at http://localhost:8080/
,但 Spring 的“上下文”的连接似乎不适用于集成测试 ( ControllerTest.java
):
java.lang.AssertionError: Status
Expected :200
Actual :404
What is wrong with the project layout, and/or the configuration annotationsin 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.
src/main/webapp/
故意丢失,以及诸如web.xml
和之类的东西WEB-INF/
。这里的目标是使用最少的配置,通过集成测试来测试驱动应用程序的视图和控制器的开发。
Gory details below. Sorry in advance for the "wall of text."
血腥细节如下。提前为“文字墙”道歉。
pom.xml
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
应用程序.java
package hello;
// ...
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
Controller.java
控制器.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
索引.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<p>Hello world!</p>
</body>
</html>
回答by Tommy Stanton
Thanks to @M.Deinum for helping me to realize that:
感谢@M.Deinum 帮助我意识到:
@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.
我想这@ContextConfiguration
是用于Spring 中的集成测试,而@SpringApplicationConfiguration
用于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.
用于确定如何为集成测试加载和配置 ApplicationContext 的类级别注释。
与标准的@ContextConfiguration 类似,但使用 Spring Boot 的 SpringApplicationContextLoader。
回答by user2758406
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");
}
}