【问题标题】:Start the IntegrationTest by calling REST endpoint in SpringBoot通过在 Spring Boot 中调用 REST 端点来启动集成测试
【发布时间】:2021-01-21 23:55:25
【问题描述】:

我假设这是可能的,并且不违反标准/不良做法。

我们正在 SpringBoot @SpringBootTest 中为微服务编写端到端测试,这种端到端测试位于单独的模块/项目中(如微服务)。

计划是编写一个带有端点的控制器并将配置文件作为参数,以编程方式运行测试@SpringBootTest

问题:

  1. 我们如何以上面指定的方式运行测试。
  2. 我们是否仍应将测试保留在 src/test 而不是 src/main 中,因为在端到端 Spring Boot 应用程序中,它所拥有的只是一个带有一些辅助类的测试类 (@SpringBootTest)?李>
  3. 还有其他更好的方法/方法/标准来组织这个吗?

【问题讨论】:

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


【解决方案1】:

问题 #1:请参阅下面的示例测试。

问题 #2:最佳做法是将测试保存在 src/test 中。

问题 #3:不,我认为你很适合。

TestRestTemplate 与 WebTestClient

注意:从 5.0 开始,此类处于维护模式,只有次要 未来接受更改和错误的请求。请, 考虑使用 org.springframework.web.reactive.client.WebClient 它具有更现代的 API 并支持同步、异步和流式传输 场景。

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

创建一个多模块项目:https://spring.io/guides/gs/multi-module/

package no.mycompany.myapp.user;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.reactive.server.WebTestClient;

@ActiveProfiles("test")
@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class LoginControllerTest {

    private static final String API_1_0_LOGIN = "/api/1.0/login";

    @Autowired
    WebTestClient webTestClient;

    @Autowired
    UserRepository userRepository;

    @BeforeEach
    public void cleanup(){
        userRepository.deleteAll();
    }

    @Test
    public void postLogin_withoutUserCredentials_receiveUnauthorized() {
        webTestClient.post()
                .uri(API_1_0_LOGIN)
                .exchange()
                .expectStatus().isUnauthorized();
    }

    ...
}

请注意,WebTestClient 需要此依赖项

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <scope>test</scope>
    </dependency>

【讨论】:

  • 上面的例子是如何测试控制器/调用控制器端点。但我希望能够从控制器开始测试。但是,测试@SpringBootTest 类位于/src/test
【解决方案2】:

一般来说,端到端测试我更喜欢黄瓜测试解决方案。

所以用 Cucumber 的方式回答你的问题:

  1. 这是a simple example,关于如何实施和触发测试。
  2. 创建一个单独的模块,如果我们想重用步骤定义,请将步骤移动到单独的项目中。
  3. 我认为 Cucumber 作为端到端测试是更好的解决方案。

更多详情:

我们可以创建一个单独的项目来定义可重用的步骤,然后在API项目中创建一个单独的模块来存储特征,并将步骤定义项目添加为依赖项。

我们从黄瓜解决方案中得到的好处是Gherkin的简单语法,使测试的编写和维护非常高效,甚至我们的BA也可以在阅读几个示例后编写测试。我们对迄今为止的表现非常满意。

以下列表是我们用于黄瓜步骤定义项目的依赖项:

  • io.cucumber:cucumber-java
  • io.cucumber:cucumber-junit(使用 junit 触发功能)
  • io.cucumber:cucumber-spring(所以我们可以使用 spring 上下文。例如 ContextConfiguration)
  • com.jayway.jsonpath:json-path(jsonpath 可以方便地访问您想要的特定结果)

【讨论】:

  • 虽然,我喜欢 Cucumber 测试,但团队不够熟练/不习惯使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
相关资源
最近更新 更多