java Sping Boot + Thymeleaf的@ WebApp配置和@上下文配置

xu3bshqb  于 2023-02-15  发布在  Java
关注(0)|答案(3)|浏览(145)

给定一个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

......用户在http://localhost:8080/上可以很好地看到"Hello World!",但是Spring的"上下文"连接似乎并不适用于集成测试(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());
  }
}

索引. html

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World!</title>
  </head>
  <body>
    <p>Hello world!</p>
  </body>
</html>
3b6akqbq

3b6akqbq1#

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

jfgube3f

jfgube3f2#

如果使用 Boot ,则可以使用@SpringBootTest或测试切片之一(在您的情况下为@WebMvcTest)https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix.test-auto-configuration.slices
如果使用Spring核心,则可以使用@WebappConfiguration + @ContextConfiguration

wgx48brx

wgx48brx3#

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

相关问题