Spring boot 对Spring Boot Rest API进行端到端测试的最佳方法是什么?

Spring boot 对Spring Boot Rest API进行端到端测试的最佳方法是什么?,spring-boot,junit,spring-boot-test,Spring Boot,Junit,Spring Boot Test,我已经在google和stackoverflow中检查了SpringBootRESTAPI的端到端测试 我想立即测试控制器->服务->存储库 我发现有两种方法是有效的: 1. TestRestTemplate: Single call to the controller method with in-memory database configuration for repository. 2. MockMvc: Create 2 Test classes. one fo

我已经在google和stackoverflow中检查了SpringBootRESTAPI的端到端测试

我想立即测试控制器->服务->存储库

我发现有两种方法是有效的:

1. TestRestTemplate: 
   Single call to the controller method with in-memory database configuration for repository.
2. MockMvc: 
   Create 2 Test classes. 
   one for Controller->Service 
   and one for Service->Repository. 
   Is there any way to club both 2 classes into one class.

从以上两种方法中,哪一种是对Spring Boot Rest API进行“端到端测试”的最佳方法?

我认为进行END2END测试的更好方法是使用TestRestTemplate。它实际上会在一个随机端口(您配置的端口)启动服务器,并调用api本身进行测试。这样,它会模拟正在运行的服务器的实际行为,并使用默认配置和bean

根据我的测试经验,MockMvc主要用于测试web层,即控制器。我将使用模拟的服务bean来替换原来的服务bean。因此,我可以测试控制器层本身的行为,而不必担心服务层中的错误。更重要的是,我不必在测试前设置任何数据库


所以我想说,对于E2E测试,您可以选择第一种方法。

Rest assured()可以作为一种选择,如果您希望通过点击http端点搜索来测试Rest API的集成测试。您所看到的是对服务的各个方面进行单元测试。如果您希望“一次完成所有任务”,那么集成测试就是其中之一。从上述两种方法(TestRestTemplate、MockMvc)中,哪一种是最好的?