简体   繁体   English

这是在 Spring Boot 中进行集成测试的最佳方法吗?

[英]Is it the best approach to do an integration test in spring boot?

Inside a project, I have a service A that uses beans B1 and B2.在一个项目中,我有一个使用 bean B1 和 B2 的服务 A。 If I want to create an integration test for the service A, I usually create the test class like this:如果我想为服务A创建一个集成测试,我通常会像这样创建测试类:

@SpringBootTest(classes = {A.class, B1.class, B2.class})
@ActiveProfiles("test")
class AIntegrationTest { 
...
}

My question: is this approach fine or is there a clean solution?我的问题:这种方法好还是有一个干净的解决方案?

Ideally, you'd want to get rid of classes = {A.class, B1.class, B2.class} and just use the @SpringBootTest annotation.理想情况下,您希望摆脱classes = {A.class, B1.class, B2.class}并只使用 @SpringBootTest 注释。 This will ensure that the same context will be built for test and for production .这将确保为testproduction构建相同的上下文。

What is more, the context will be cached by the spring test runner in this case and will be reused by tests, which require the same kind of context.更重要的是,在这种情况下,上下文将被 spring 测试运行器缓存,并被需要相同类型上下文的测试重用。

Not sure you even need to define the beans in the @SpringBootTest annotation.不确定您甚至需要在 @SpringBootTest 注释中定义 bean。 either @Mock the classes with mockito to @Autowire them要么@Mock 类与mockito 到@Autowire 他们

// here's an example from the web
@RunWith(SpringRunner.class)
@SpringBootTest(
  SpringBootTest.WebEnvironment.MOCK,
  classes = Application.class)
@AutoConfigureMockMvc
@TestPropertySource(
  locations = "classpath:application-integrationtest.properties")
public class EmployeeRestControllerIntegrationTest {

    @Autowired
    private MockMvc mvc;

    @Autowired
    private EmployeeRepository repository;

    // write test cases here
}

Here's another example from the spring docs.这是 spring 文档中的另一个示例。

package com.example.testingweb;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {

    @LocalServerPort
    private int port;

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void greetingShouldReturnDefaultMessage() throws Exception {
        assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/",
                String.class)).contains("Hello, World");
    }
}

I would recommend to avoid writing integration tests using SpringBoot for multiple reasons, including but not limited to the time it takes to run integration tests.出于多种原因,我建议避免使用 SpringBoot 编写集成测试,包括但不限于运行集成测试所需的时间。 Some of the problems can be read here https://medium.com/swlh/the-devil-that-is-springboottest-68b7f4148bb6一些问题可以在这里阅读https://medium.com/swlh/the-devil-that-is-springboottest-68b7f4148bb6

The best approach would be to have an application deployed in test environment and create a suite of automated tests using Cypress , Postman etc.最好的方法是在测试环境中部署一个应用程序,并使用CypressPostman等创建一套自动化测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM