使用 MockMvc 和 SpringBootTest 和使用 WebMvcTest 的区别 [英] Difference between using MockMvc with SpringBootTest and Using WebMvcTest

查看:48
本文介绍了使用 MockMvc 和 SpringBootTest 和使用 WebMvcTest 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring Boot 的新手,正在尝试了解 SpringBoot 中的测试是如何工作的.我对以下两个代码片段之间的区别有些困惑:

I am new to Spring Boot and am trying to understand how testing works in SpringBoot. I am a bit confused about what is the difference between the following two code snippets:

代码片段 1:

@RunWith(SpringRunner.class)
@WebMvcTest(HelloController.class)
public class HelloControllerApplicationTest {
    @Autowired    
    private MockMvc mvc;

    @Test
    public void getHello() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
    }
}

这个测试使用了 @WebMvcTest 注释,我认为它是用于功能切片测试并且只测试 Web 应用程序的 MVC 层.

This test uses the @WebMvcTest annotation which I believe is for feature slice testing and only tests the MVC layer of a web application.

代码片段 2:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {

    @Autowired
    private MockMvc mvc;

    @Test
    public void getHello() throws Exception {
    mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
    }
}

这个测试使用了 @SpringBootTest 注释和一个 MockMvc.那么这与代码片段 1 有何不同?这有什么不同?

This test uses the @SpringBootTest annotation and a MockMvc. So how is this different from code snippet 1? What does this do differently?

添加代码片段 3(在 Spring 文档中发现这是一个集成测试示例)

Adding Code Snippet 3 (Found this as an example of integration testing in the Spring documentation)

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class HelloControllerIT {
    
    @LocalServerPort private int port;
    private URL base;
    
    @Autowired private TestRestTemplate template;
    
    @Before public void setUp() throws Exception {
        this.base = new URL("http://localhost:" + port + "/");
    }
    
    @Test public void getHello() throws Exception {
        ResponseEntity < String > response = template.getForEntity(base.toString(), String.class);
        assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
    }
}

推荐答案

@SpringBootTest 是通用的测试注解.如果您正在寻找在 1.4 之前执行相同操作的东西,那么您应该使用它.它根本不使用切片,这意味着它将启动您的完整应用程序上下文,而根本不会自定义组件扫描.

@SpringBootTest is the general test annotation. If you're looking for something that does the same thing prior to 1.4, that's the one you should use. It does not use slicing at all which means it'll start your full application context and not customize component scanning at all.

@WebMvcTest 只会扫描您定义的控制器和 MVC 基础设施.就是这样.因此,如果您的控制器对来自您的服务层的其他 bean 有某种依赖性,则在您自己加载该配置或为其提供模拟之前,测试不会开始.这要快得多,因为我们只加载您应用程序的一小部分.此注释使用切片.

@WebMvcTest is only going to scan the controller you've defined and the MVC infrastructure. That's it. So if your controller has some dependency to other beans from your service layer, the test won't start until you either load that config yourself or provide a mock for it. This is much faster as we only load a tiny portion of your app. This annotation uses slicing.

阅读文档 应该也能帮到你.

这篇关于使用 MockMvc 和 SpringBootTest 和使用 WebMvcTest 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆