java – @WebAppConfiguration和Spring Boot Thymeleaf的@ContextConfiguration

前端之家收集整理的这篇文章主要介绍了java – @WebAppConfiguration和Spring Boot Thymeleaf的@ContextConfiguration前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
给定一个 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.xml和WEB-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>

解决方法

感谢@ 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

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.

原文链接:https://www.f2er.com/java/129812.html

猜你在找的Java相关文章