0
0
JUnittesting~10 mins

@Tag for categorization in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses the @Tag annotation to categorize tests. It verifies that a simple addition method returns the correct result.

Test Code - JUnit
JUnit
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {

    @Tag("math")
    @Test
    void testAddition() {
        int result = add(2, 3);
        assertEquals(5, result, "2 + 3 should equal 5");
    }

    int add(int a, int b) {
        return a + b;
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test runner starts and identifies tests with @Tag("math")JUnit test environment ready, CalculatorTest loaded-PASS
2JUnit runs testAddition() methodInside testAddition method, variables initialized-PASS
3Calls add(2, 3) methodadd method executes and returns 5-PASS
4Assert that result equals 5Result is 5, expected is 5assertEquals(5, result)PASS
5Test completes successfullyTest marked as passed in report-PASS
Failure Scenario
Failing Condition: The add method returns incorrect result (e.g., 4 instead of 5)
Execution Trace Quiz - 3 Questions
Test your understanding
What is the purpose of the @Tag("math") annotation in this test?
ATo mark the test as ignored
BTo categorize the test for selective execution
CTo specify the test method name
DTo set the test timeout
Key Result
Using @Tag allows you to group and run specific categories of tests easily, improving test management and execution control.