Recall & Review
beginner
What is the purpose of the @Order annotation in JUnit?
The @Order annotation specifies the order in which test methods are executed within a test class. It helps control test execution sequence.
Click to reveal answer
beginner
How do you use @Order to run a test method first?
You add @Order(1) above the test method. Lower numbers run before higher numbers.
Click to reveal answer
beginner
Can @Order be used without specifying a test method order?
No. @Order requires a numeric value to define the execution order. Without a value, it has no effect.
Click to reveal answer
intermediate
What happens if two test methods have the same @Order value?
JUnit runs them in an undefined order relative to each other. It's best to use unique order values to avoid confusion.
Click to reveal answer
beginner
Write a simple example of using @Order in a JUnit 5 test class.
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
@TestMethodOrder(OrderAnnotation.class)
class MyTests {
@Test
@Order(2)
void secondTest() {
Assertions.assertTrue(true);
}
@Test
@Order(1)
void firstTest() {
Assertions.assertTrue(true);
}
}Click to reveal answer
What annotation must be added to a JUnit 5 test class to enable @Order on test methods?
✗ Incorrect
You must add @TestMethodOrder(OrderAnnotation.class) to the test class to tell JUnit to use the @Order annotation for method execution order.
If a test method has @Order(3), when will it run compared to a method with @Order(1)?
✗ Incorrect
Lower @Order values run first, so @Order(3) runs after @Order(1).
What happens if you do not use @TestMethodOrder but use @Order on test methods?
✗ Incorrect
Without @TestMethodOrder(OrderAnnotation.class), JUnit ignores @Order and runs tests in default order.
Which of these is a valid @Order value?
✗ Incorrect
@Order requires an integer value to specify execution order.
Why might you want to control test execution order with @Order?
✗ Incorrect
You use @Order to run tests in a specific sequence when some tests depend on others or for logical flow.
Explain how to use @Order to control the execution order of test methods in JUnit 5.
Think about the class-level annotation and method-level annotations.
You got /4 concepts.
What are the consequences of having duplicate @Order values on test methods?
Consider how JUnit handles ties in order values.
You got /3 concepts.