0
0
JUnittesting~10 mins

Test method naming conventions in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a simple addition method works correctly. It uses a clear and descriptive test method name to show what is being tested and what the expected result is.

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

public class CalculatorTest {

    @Test
    void addition_of_two_positive_numbers_should_return_correct_sum() {
        Calculator calc = new Calculator();
        int result = calc.add(2, 3);
        assertEquals(5, result, "2 + 3 should equal 5");
    }
}

class Calculator {
    int add(int a, int b) {
        return a + b;
    }
}
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test runner starts and looks for test methodsJUnit identifies method named 'addition_of_two_positive_numbers_should_return_correct_sum' with @Test annotation-PASS
2JUnit creates Calculator instance and calls add(2, 3)Calculator object ready, add method invoked with inputs 2 and 3-PASS
3JUnit asserts that result equals 5Result from add method is 5assertEquals(5, result)PASS
4Test completes successfullyNo exceptions thrown, test method finished-PASS
Failure Scenario
Failing Condition: If the add method returns a wrong sum, e.g., 4 instead of 5
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test method name 'addition_of_two_positive_numbers_should_return_correct_sum' tell us?
AIt is a random name with no meaning
BIt describes what is tested and the expected result
CIt only tells the input values
DIt shows the test framework used
Key Result
Using clear and descriptive test method names helps anyone reading the test understand what is tested and what the expected behavior is, improving test maintainability and communication.