0
0
JUnittesting~20 mins

@Order for execution order in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit @Order Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Test execution order with @Order annotation
Given the following JUnit 5 test class, what is the order of test method execution?
JUnit
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;

@TestMethodOrder(OrderAnnotation.class)
public class SampleTest {

    @Test
    @Order(2)
    void testSecond() {
        System.out.println("Second");
    }

    @Test
    @Order(1)
    void testFirst() {
        System.out.println("First");
    }

    @Test
    @Order(3)
    void testThird() {
        System.out.println("Third");
    }
}
AFirst, Second, Third
BSecond, First, Third
CThird, Second, First
DExecution order is random
Attempts:
2 left
💡 Hint
Look at the @Order values and the @TestMethodOrder annotation.
assertion
intermediate
2:00remaining
Assertion to verify test execution order
Which assertion correctly verifies that a list of executed test names matches the expected order when using @Order annotation?
JUnit
List<String> executedTests = List.of("testFirst", "testSecond", "testThird");
List<String> expectedOrder = List.of("testFirst", "testSecond", "testThird");
AassertArrayEquals(expectedOrder.toArray(), executedTests.toArray());
BassertEquals(expectedOrder, executedTests);
CassertTrue(executedTests.containsAll(expectedOrder));
DassertNotNull(executedTests);
Attempts:
2 left
💡 Hint
Check which assertion verifies both order and content equality.
🔧 Debug
advanced
2:00remaining
Why is @Order ignored in this test class?
Consider this JUnit 5 test class: import org.junit.jupiter.api.*; public class TestClass { @Test @Order(1) void testA() {} @Test @Order(2) void testB() {} } Why are the tests not executed in the order testA then testB?
ABecause @Order values must be unique
BBecause @Order annotation is only for JUnit 4
CBecause test methods must be static to use @Order
DBecause @TestMethodOrder(OrderAnnotation.class) is missing on the class
Attempts:
2 left
💡 Hint
Check if the class has the annotation that enables ordering by @Order.
🧠 Conceptual
advanced
2:00remaining
Effect of duplicate @Order values
What happens if two test methods in a JUnit 5 test class have the same @Order value when using @TestMethodOrder(OrderAnnotation.class)?
AJUnit runs tests alphabetically ignoring @Order
BJUnit throws a runtime exception and stops execution
CTests with the same order value run in an undefined order relative to each other
DTests with duplicate order values are skipped
Attempts:
2 left
💡 Hint
Think about how ordering works when priorities are equal.
framework
expert
2:00remaining
Combining @Order with other TestMethodOrderers
Given a JUnit 5 test class annotated with @TestMethodOrder(MethodOrderer.OrderAnnotation.class), what happens if a test method is missing the @Order annotation?
AThe test method is executed before all methods with @Order annotations
BJUnit throws an exception and fails the test run
CThe test method is executed after all methods with @Order annotations
DThe test method is ignored and not executed
Attempts:
2 left
💡 Hint
The default @Order value is 0, and lower values run first.