Spring Boot Rest API 端到端测试的最佳方式是什么?

Which is the best way to do end to end testing of Spring Boot Rest APIs?

我已经在 google 和 Whosebug 中检查了 Spring Boot Rest API 的端到端测试。

我想立即测试 controller -> Service -> Repository。

我发现有两种方法很有效:

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.

哪种方法是通过上述 2 种方法执行 "end to end testing" 的 Spring Boot Rest API 的最佳方法???

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

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

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