For this specific problem, a hierachical order is better than a sequential order.
If you are using JUnit 5, you can use @Nested tests. https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested
Nest your Test classes in a way that the Parent Test class runs first and the nested Test can use the test objects created by the parent.
Here is a high-level example:
class AirportControllerTest{
@Test
void testAirportController() {
//Add parent here
}
@Nested
class RouteControllerTest {
@Test
void testRouteController() {
//Use test objects from parent here
}
@Nested
class FlightControllerTest{
@Test
void testFlightController() {
//Use test objects from AirportControllerTest & RouteControllerTest
}
}
}
If you are using JUnit 4, you can enclose your Test Classes in a Test Suite. This answer is a good match - https://stackoverflow.com/a/42600619/6352160
Less relevant: For configuring the Test Method order within a Test Class.
If you are using JUnit5, specify @TestMethodOrder in your Test class and use @Order annotation on methods to specify their order of execution. Reference - https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/TestMethodOrder.html.
If using JUnit4, this question has several possible solutions - How to run test methods in specific order in JUnit4?