spring-boot - TestRestTemplate 与 WebTestClient 与 RestAssured : What is the best approach for integration testing of Spring Boot Rest API

标签 spring-boot rest testing

3 个 API 中的哪一个:TestRestTemplate、WebTestClient、RestAssured 对于 Spring Boot Rest API(非响应式)的集成测试在以下方面最有效:

  • 确保与 Spring boot 的增量版本兼容。
  • 易于使用且广泛的断言,用于检查状态代码、 header 、JSON/XML 有效负载验证、响应时间等。
  • 有可能测试 spring 容器配置。
  • 可在所有端点重复使用特定检查的功能。
  • 是最稳定的,因此是行业通用的做法。

  • 很少有初步发现可以阻止其中一个被标记为明显的赢家:
  • TestRestTemplate 和 WebTestClient 是 Spring 内置 API,因此可以使用 Spring Boot 自动配置。最新版本的 RestAssured(4.0.0 以上)可能会出现 spring boot( java.lang.NoClassDefFoundError: io/restassured/mapper/factory/GsonObjectMapperFactory ) 问题并且需要显式添加依赖项。
  • WebTestClient(一个 Fluent API)与 TestRestTemplate 相比似乎具有很好且易于使用的语法,但它是非常新的,并且没有足够的示例显示其对非 react 性 Rest API 的使用以及广泛的断言 API 的可用性
  • RestTemplate(TestRestTemplate 所基于的 API)将在 future 的 Spring Boot 版本中被弃用。

  • 经验丰富的开发人员的反馈将是一个很大的帮助。

    最佳答案

    我选择了 WebTestClient,我很满意。正如你所写的,WebTestClient 是内置的 Spring Boot,所以这是我开始使用 WebTestClient 的主要原因,因为如果我有很好的开箱即用的解决方案,我不想添加另一个依赖项。无论您的应用程序是响应式还是不响应式,您都可以使用 WebTestClient。它支持 JSONPath。我也有使用 TestRestTemplate 的经验,但我认为 WebTestClient 更好。

    关于spring-boot - TestRestTemplate 与 WebTestClient 与 RestAssured : What is the best approach for integration testing of Spring Boot Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61318756/

    相关文章:

    grails - Spock错误groovy.lang.MissingMethodException:当一种方法调用另一种方法时

    java - Spring Boot 1.5.2 启动 : java. lang.NoSuchMethodError : javax. servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

    java - 使用附加参数重写无状态 Spring 服务中的方法

    c# - 在 C# restful web 服务中删除 xml 命名空间返回字符串

    php - Paypal REST Api 错误 : response 401 PPConnectionException

    android - 为什么 google 声明 UI 测试时应禁用动画?

    java - 如何使用 JdbcTemplate 正确处理此日期字段映射到 Spring Boot 项目中?

    java - 如何在 Spring 框架中提供 robots.txt 服务?

    javascript - AngularJS Controller 工厂剩余 JSON

    JavaScript:在另一个函数调用完成后继续执行函数