0
0
JUnittesting~10 mins

Test suites with @Suite in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test suite groups two simple test classes using JUnit's @Suite annotation. It verifies that all tests in the suite run and pass successfully.

Test Code - JUnit 5
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestClassOne {
    @Test
    void testOne() {
        assertTrue(1 + 1 == 2);
    }
}

public class TestClassTwo {
    @Test
    void testTwo() {
        assertTrue("hello".startsWith("h"));
    }
}

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({TestClassOne.class, TestClassTwo.class})
public class MyTestSuite {
    // This class remains empty, used only as a holder for the above annotations
}
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test runner starts and loads MyTestSuite classJUnit test runner initialized with suite containing TestClassOne and TestClassTwo-PASS
2Test runner executes TestClassOne.testOne()TestClassOne loaded, testOne method ready to runassertTrue(1 + 1 == 2) verifies 2 equals 2PASS
3Test runner executes TestClassTwo.testTwo()TestClassTwo loaded, testTwo method ready to runassertTrue("hello".startsWith("h")) verifies string starts with 'h'PASS
4Test runner completes execution of all tests in suiteAll tests in MyTestSuite executedAll assertions passedPASS
Failure Scenario
Failing Condition: One of the test methods in the suite fails its assertion
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @Suite annotation do in this test code?
AGroups multiple test classes to run together as one suite
BMarks a single test method to be skipped
CDefines a test class as a parameterized test
DSpecifies the order of test execution inside a class
Key Result
Using @Suite in JUnit helps organize multiple test classes to run together, making it easier to manage related tests and see combined results.