0
0
JUnittesting~10 mins

First JUnit test - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if the addition of two numbers works correctly using JUnit. It verifies that 2 + 3 equals 5.

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 result = 2 + 3;
        assertEquals(5, result, "2 + 3 should equal 5");
    }
}
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner initializes the CalculatorTest class-PASS
2Runs testAddition methodInside testAddition method, calculation 2 + 3 is performed-PASS
3Calls assertEquals to check if result equals 5assertEquals compares expected value 5 with actual result 5assertEquals(5, result) verifies 5 == 5PASS
4Test completes successfullyTest runner marks testAddition as passed-PASS
Failure Scenario
Failing Condition: If the addition result is not 5, for example if result is 4
Execution Trace Quiz - 3 Questions
Test your understanding
What does the assertEquals method check in this test?
AIf the actual result equals the expected value 5
BIf the test method runs without errors
CIf the CalculatorTest class compiles
DIf the test runner starts correctly
Key Result
Always include clear assertions in your tests to verify expected outcomes. This helps catch errors early and ensures your code works as intended.