0
0
JUnittesting~10 mins

assertTrue and assertFalse in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a number is positive using assertTrue and if a number is negative using assertFalse. It verifies that the conditions about the number's sign are correct.

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

public class NumberSignTest {

    @Test
    public void testNumberSign() {
        int number = 5;
        assertTrue(number > 0, "Number should be positive");

        int negativeNumber = -3;
        assertFalse(negativeNumber > 0, "Number should not be positive");
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner is ready to execute the test method-PASS
2Assign number = 5Variable 'number' holds value 5-PASS
3assertTrue(number > 0)Check if 5 > 0Verify condition 'number > 0' is truePASS
4Assign negativeNumber = -3Variable 'negativeNumber' holds value -3-PASS
5assertFalse(negativeNumber > 0)Check if -3 > 0Verify condition 'negativeNumber > 0' is falsePASS
6Test ends successfullyAll assertions passed-PASS
Failure Scenario
Failing Condition: If number is not greater than 0 for assertTrue or if negativeNumber is greater than 0 for assertFalse
Execution Trace Quiz - 3 Questions
Test your understanding
What does assertTrue(number > 0) check in the test?
AThat number is positive
BThat number is negative
CThat number equals zero
DThat number is less than zero
Key Result
Use assertTrue to confirm a condition is true and assertFalse to confirm a condition is false. This helps clearly check expected boolean conditions in tests.