Spring Boot + Thymeleaf的@WebAppConfiguration和@ContextConfiguration

时间:2016-01-05 08:01:04

标签: java spring spring-boot integration-testing thymeleaf

鉴于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

...#34; Hello World!"用户受到了很好的欢迎!在http://localhost:8080/,但是Spring" context"似乎不适用于集成测试(ControllerTest.java):

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>

2 个答案:

答案 0 :(得分:10)

感谢@ 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 for the latter

  

类级别注释,用于确定如何为集成测试加载和配置ApplicationContext。

     

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

答案 1 :(得分:0)

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