课程: Complete Guide To Java Testing with JUnit 5 & Mockito

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Control test execution with @Order

Control test execution with @Order

- [Instructor] When writing tests for a given class, tests might need to run in a specific sequence for certain scenarios. Although tests should ideally be independent of each other, there are times when the order of execution impacts the test results, specifically for integration tests. In JUnit 5, you can control the sequence using the @Order annotation. Let's take a look at an example. Here, we have a test class for UserManagementTests. These tests cover the functionality for managing a user. There's a test for updating a user, creating a user, retrieving a user, and deleting a user. Let's run these tests. Two of the four tests fail. All of these tests share a test user. This means the order of the test matters. We'll need to create the user before we try to retrieve it or update it, and we'll need to make sure that the deletion happens last. Let's use the order annotation to ensure these tests are executed properly. The first step is to add the @TestMethodOrder annotation. We'll…

内容