gpt4 book ai didi

java - Spring Boot + Thymeleaf 的@WebAppConfiguration 和@ContextConfiguration

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:44:46 28 4
gpt4 key购买 nike

给定一个 Spring Boot + Thymeleaf Web 应用程序(这与 Spring 项目的 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

...用户会收到“Hello World!”的问候语。在 http://localhost:8080/,但 Spring 的“上下文”的连接似乎不适用于集成测试(ControllerTest.java):

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

测试中的项目布局和/或配置注释有什么问题?

src/main/webapp/ 是有意缺失的,还有 web.xmlWEB-INF/ 之类的东西。这里的目标是使用最少的配置,通过集成测试来测试驱动应用程序的 View 和 Controller 的开发。

血淋淋的细节如下。提前为“文字墙”道歉。


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>

应用程序.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 {
}

配置.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>

最佳答案

感谢@M.Deinum 帮助我意识到这一点:

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

...应该是:

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

我想 @ContextConfiguration 用于 Spring 中的集成测试,而 @SpringApplicationConfiguration 用于 Spring 中的集成测试启动

根据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.

关于java - Spring Boot + Thymeleaf 的@WebAppConfiguration 和@ContextConfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34606760/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com