0
0
JUnittesting~10 mins

@Test annotation in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses the @Test annotation to mark a method as a test case. It verifies that the addition of two numbers returns the correct result.

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

public class CalculatorTest {

    @Test
    public void testAddition() {
        int a = 5;
        int b = 3;
        int result = a + b;
        assertEquals(8, result, "5 + 3 should equal 8");
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1JUnit test runner starts and scans for methods annotated with @TestTest class CalculatorTest loaded with method testAddition marked as @Test-PASS
2JUnit invokes the testAddition() methodInside testAddition method, variables a=5 and b=3 initialized-PASS
3Calculate result = a + bresult variable holds value 8-PASS
4Assert that result equals 8 using assertEqualsComparing expected value 8 with actual result 8assertEquals(8, result) verifies values are equalPASS
5JUnit marks testAddition as PASSED and finishes test runTest report shows testAddition PASSED-PASS
Failure Scenario
Failing Condition: If the addition result is not equal to 8
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @Test annotation do in this code?
AMarks the method as a test case to be run by JUnit
BDeclares a variable
CImports the JUnit library
DRuns the main application
Key Result
Using the @Test annotation clearly marks test methods, allowing the test runner to find and execute them automatically. This keeps tests organized and easy to run.