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.
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.
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"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner is ready to execute the test method | — | PASS |
| 2 | Assign number = 5 | Variable 'number' holds value 5 | — | PASS |
| 3 | assertTrue(number > 0) | Check if 5 > 0 | Verify condition 'number > 0' is true | PASS |
| 4 | Assign negativeNumber = -3 | Variable 'negativeNumber' holds value -3 | — | PASS |
| 5 | assertFalse(negativeNumber > 0) | Check if -3 > 0 | Verify condition 'negativeNumber > 0' is false | PASS |
| 6 | Test ends successfully | All assertions passed | — | PASS |
assertTrue(condition) check in a test?assertTrue is designed to verify that a given boolean condition is true during a test.assertFalse which checks for false, assertTrue specifically expects the condition to be true.isValid is false in JUnit?isValid is false, we use assertFalse with the variable directly.assertFalse(isValid); correctly asserts the variable is false. Other options use the wrong assertion method or incorrect comparisons that fail to properly verify the variable is false.boolean isActive = (5 > 3);
assertTrue(isActive);
assertFalse(isActive == false);
5 > 3 is true, so isActive is true.assertTrue(isActive); passes because isActive is true.assertFalse(isActive == false); also passes because isActive == false is false, so assertFalse(false) passes.boolean isReady = false;
assertTrue(isReady);
isReady is set to false.assertTrue(isReady); expects the condition to be true, but it is false, so the assertion fails.isEmpty(String s) returns false when the string is not empty. Which assertion correctly tests this?isEmpty("hello") should return false because the string is not empty.assertFalse(isEmpty("hello")); which passes if the method returns false.Pass the test and this pattern moves unknown