0
0
JUnittesting~10 mins

Deterministic tests in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks that a simple calculator's addition method always returns the same result for the same inputs. It verifies the test is deterministic, meaning it produces consistent results every time it runs.

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

public class CalculatorTest {

    static class Calculator {
        int add(int a, int b) {
            return a + b;
        }
    }

    @Test
    void testAdditionIsDeterministic() {
        Calculator calc = new Calculator();
        int result1 = calc.add(2, 3);
        int result2 = calc.add(2, 3);
        assertEquals(result1, result2, "Addition results should be the same for same inputs");
        assertEquals(5, result1, "2 + 3 should equal 5");
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner initialized-PASS
2Calculator object is createdCalculator instance ready-PASS
3Call add(2, 3) first timeMethod returns 5-PASS
4Call add(2, 3) second timeMethod returns 5 again-PASS
5Assert result1 equals result2Both results are 5Check if 5 == 5PASS
6Assert result1 equals 5Result1 is 5Check if 5 == 5PASS
7Test ends successfullyTest passed with no errors-PASS
Failure Scenario
Failing Condition: The add method returns different results for the same inputs
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the add method?
AIt always returns the same result for the same inputs
BIt returns random results
CIt throws an exception
DIt modifies input values
Key Result
Deterministic tests always produce the same result for the same inputs, making debugging easier and tests reliable.