@Spring Boot + Thymeleaf中的@WebAppConfiguration和@ContextConfiguration

11

给定一个Spring Boot + Thymeleaf Web应用程序(这与Spring项目的 gs-consuming-rest "initial" 代码树 几乎相同):

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── hello
    │   │       ├── Application.java
    │   │       ├── Config.java
    │   │       └── Controller.java
    │   └── resources
    │       └── templates
    │           └── index.html
    └── test
        └── java
            └── hello
                └── ControllerTest.java

...用户通过 http://localhost:8080/ 得到了 "Hello, World!" 的问候,但是在集成测试中(ControllerTest.java),Spring 的 "context" 配置似乎没有生效:

java.lang.AssertionError: Status 
Expected :200
Actual   :404
在测试中,项目布局或配置注释出了什么问题? src/main/webapp/ 故意省略了,以及像 web.xmlWEB-INF/ 这样的东西。目标是使用最少的配置,通过集成测试来驱动视图和控制器的开发。
以下是详细内容。抱歉提前给你带来 "大段文本" 的不便。

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>

1
你不应该使用@ContextConfiguration,而是应该使用@SpringApplicationConfiguration,并将其指向你的应用程序类,而不是配置类。 - M. Deinum
@M.Deinum 我使用 @SpringApplicationConfiguration。我如何在测试中使用不同的 @Configuration 类? - Xolve
3个回答

14

感谢@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 Boot 的集成测试。

根据后者的 Javadoc:

  

Class-level 注解,用于确定如何为集成测试加载和配置 ApplicationContext。

     

类似于标准的 @ContextConfiguration,但使用 Spring Boot 的 SpringApplicationContextLoader。


4
@SpringApplicationConfiguration已经被弃用,现在推荐使用其他方式:https://dev59.com/FFkS5IYBdhLWcg3w05eT - Noumenon
我遇到了同样的问题,但我正在使用@SpringBootTest... 有什么解决办法吗? - thevipulvats

0

-1
    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");
    }
}

这会加载应用程序的全部上下文以供 TestClass 使用。 - user2758406

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接