0
0
JUnittesting~10 mins

Test naming conventions deep dive in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates how to name JUnit test methods clearly to describe what they verify. It checks that a simple calculator adds numbers correctly and that the test name reflects the behavior being tested.

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

public class CalculatorTest {

    @Test
    void add_TwoPositiveNumbers_ReturnsCorrectSum() {
        Calculator calc = new Calculator();
        int result = calc.add(3, 5);
        assertEquals(8, result, "Sum should be 8");
    }
}

class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test runner starts and loads CalculatorTest classJUnit test environment initialized-PASS
2JUnit finds method add_TwoPositiveNumbers_ReturnsCorrectSum annotated with @TestTest method ready to execute-PASS
3Test method creates Calculator instanceCalculator object instantiated-PASS
4Test method calls add(3, 5) on CalculatorCalculator computes sum 8-PASS
5Test method asserts result equals 8Assertion compares expected 8 with actual 8assertEquals(8, result)PASS
6Test method completes successfullyTest marked as passed in report-PASS
Failure Scenario
Failing Condition: Calculator add method returns incorrect sum
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test method name add_TwoPositiveNumbers_ReturnsCorrectSum tell us?
AIt describes the input and expected behavior of the test
BIt is a random name with no meaning
CIt only tells the test framework to run this method
DIt shows the test will fail
Key Result
Clear and descriptive test method names improve understanding and maintenance by explaining what behavior is tested and expected.