简体   繁体   English

应该测试Spring Boot应用程序的最佳方法是什么? 参与服务器还是通过创建模拟环境?

[英]What is the best way Spring Boot application should be tested ? With server involvement or by creating mock environment?

I want to test my Spring Boot application's layers - service, data, controllers. 我想测试我的Spring Boot应用程序的层-服务,数据,控制器。

I know with Spring Boot we can test these with server involvement or without involvement (by creating a mock environment). 我知道,通过Spring Boot,我们可以在服务器参与或不参与的情况下(通过创建模拟环境)对它们进行测试。 So my question here is - what is the best practise to test these layers? 所以我的问题是-测试这些层的最佳实践是什么? Or if it depends on the specific layer which we want to test? 还是取决于我们要测试的特定层?

Unit testing is straightforward.. you stick to SRP principle, mock out all the collaborators and test in isolation. 单元测试非常简单..您坚持SRP原则,模拟出所有协作者并进行独立测试。

When it comes to integration testing there is no clear answer and definite standard. 对于集成测试,没有明确的答案和明确的标准。 It always depends on the project you are working on and even the specific feature. 它始终取决于您正在处理的项目,甚至取决于特定功能。

After working with a few Spring Boot applications I am leaning towards splitting your IT suite into four parts/categories: 在使用了一些Spring Boot应用程序之后,我倾向于将您的IT套件分为四个部分/类别:

1) REST API Tests 1)REST API测试

So we test only the @Restontrollers . 因此,我们仅测试@Restontrollers You use @WebMvcTest(MyController.class) or @WebFluxTest(MyController.class) depending on why type of controller you use. 您使用@WebMvcTest(MyController.class)@WebFluxTest(MyController.class)具体取决于您使用的控制器类型。 Anything from the service layer down is mocked. 从服务层到底层的任何事物都将被模拟。 You put as much emphasis on inputs params/request body as possible and test the hell out of every combination. 您尽可能强调输入参数/请求正文,并从每种组合中测试地狱。

2) Repository Tests 2)存储库测试

Tests that verify the ORM layer only. 测试仅验证ORM层。 You use @DataJpaTest for this. 为此使用@DataJpaTest Here you test every possible scenario for each repository method you have, without caring what invoked it etc. 在这里,您可以针对每种存储库方法测试每种可能的情况,而无需关心调用它的内容等。

3) Slice Tests 3)切片测试

This is where you verify the interaction between your @Component and @Service beans. 在这里,您可以验证@Component@Service Bean之间的交互。 This is where the actual business logic of your app is tested. 这是测试您的应用程序实际业务逻辑的地方。 It is up to decide whether you send the data via REST or use a call to the top-most @Service . 由您决定是通过REST发送数据还是使用对最顶层@Service的调用。 I always stubbed the @Repository layer though in this case. 在这种情况下,我总是将@Repository层存根。

4) End to End Tests 4)端到端测试

These work on a real Web Server: 这些可以在真实的Web服务器上工作:

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)

Nothing is mocked here. 这里什么都没有嘲笑。

Discussion 讨论

The key here is to decide how much each of these should be used, keeping in mind that slice testing is probably the most important. 这里的关键是决定应使用每种方法的量,同时要记住切片测试可能是最重要的。

The main emphasis should NOT be put on E2E tests for sure. 当然,不应将主要重点放在E2E测试上。 They are sort of a comma at the end of a sentence. 它们在句子结尾有点像逗号。 They are the slowest and require most set-up. 它们是最慢的,需要最多的设置。 Thus they are most error-prone, hard to maintain and hard to understand. 因此,它们最容易出错,难以维护且难以理解。 Ideally, these should be kept to a bare minimum (most crucial cases covered only). 理想情况下,应将这些限制降至最低(仅涵盖最关键的情况)。

Also when running your IT suite they should be grouped in this order: 同样,在运行IT套件时,应按以下顺序将它们分组:

1) Controller Tests 1)控制器测试

2) Slice Tests 2)切片测试

3) Repository Tests 3)存储库测试

4) E2E Tests 4)端到端测试

(from the fastest and requiring least environment interaction to the slowest). (从最快的,要求最少的环境交互到最慢的交互)。

Again, it is up to the team to decide which ones of these we want to use, in what order and with what frequency. 同样,由团队决定我们要使用哪个,以什么顺序和频率使用。

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

相关问题 在 Spring Boot 应用程序中安排任务的最佳方法是什么 - What is best way to schedule task in spring boot application Spring Boot 应用程序组织的最佳实践是什么? - What are best practices for Spring Boot application organization? 在 Spring Boot 中处理异常的最佳方法是什么? - What is the best way to handle exception in Spring Boot? 部署Spring引导应用程序的正确方法是什么 - What is the correct way to deploy a spring boot application Spring 引导集成测试:模拟环境接口 - Spring Boot Integration Test : Mock Environment Interface 扩展spring应用程序上下文的最佳方法是什么? - What is the best way of extending spring application context? 在没有 Web 服务器的 Spring-Boot 应用程序中,保持它运行的正确方法是什么? - In a Spring-Boot application without web server, what is the correct way to keep it running? 将一些 JSON 文件加载到 Spring Boot 应用程序中的最佳方法 - Best way to load some JSON files into a Spring Boot application 检测 Spring Boot/Java 配置应用程序的最佳方式 - Best way to instrument Spring Boot/Java Config Application 为 spring 引导应用程序部署新功能的最佳做法是什么? - what are best practices for deploying new features for spring boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM