We use assertTrue and assertFalse to check if something is true or false in our tests. This helps us make sure our code works as expected.
assertTrue and assertFalse in JUnit
assertTrue(condition); assertFalse(condition);
condition is a boolean expression that should be true or false.
If the condition is not what you expect, the test will fail and show an error.
isUserLoggedIn() returns true.assertTrue(isUserLoggedIn());
isFeatureEnabled() returns false.assertFalse(isFeatureEnabled());
assertTrue(list.contains("apple"));
user.isBlocked() returns false.assertFalse(user.isBlocked());
This test checks two simple conditions: it expects isSunny to be true and isRaining to be false. Both assertions should pass.
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class SimpleTest { @Test void testConditions() { boolean isSunny = true; boolean isRaining = false; assertTrue(isSunny); assertFalse(isRaining); } }
Use clear and simple conditions inside assertTrue and assertFalse for easy understanding.
If an assertion fails, JUnit will report which check failed.
You can add a message as a second argument to explain the failure, like assertTrue(condition, "Must be true").
assertTrue checks if a condition is true.
assertFalse checks if a condition is false.
They help confirm your code behaves as expected by testing boolean results.
Practice
assertTrue(condition) check in a test?Solution
Step 1: Understand the purpose of assertTrue
The methodassertTrueis designed to verify that a given boolean condition is true during a test.Step 2: Compare with other assertions
UnlikeassertFalsewhich checks for false,assertTruespecifically expects the condition to be true.Final Answer:
It checks if the condition is true. -> Option BQuick Check:
assertTrue means condition must be true [OK]
- Confusing assertTrue with assertFalse
- Thinking assertTrue checks for exceptions
- Assuming assertTrue checks for null
isValid is false in JUnit?Solution
Step 1: Identify the correct assertion method
To check ifisValidis false, we useassertFalsewith the variable directly.Step 2: Evaluate the options
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.Final Answer:
assertFalse(isValid); -> Option CQuick Check:
Use assertFalse(variable) to check false [OK]
- Using assertTrue to check false
- Adding unnecessary comparisons like == true or == false
- Confusing assertTrue and assertFalse syntax
boolean isActive = (5 > 3);
assertTrue(isActive);
assertFalse(isActive == false);
What will be the result of running this test?
Solution
Step 1: Evaluate the boolean expression
The expression5 > 3is true, soisActiveis true.Step 2: Check assertions with isActive = true
assertTrue(isActive);passes because isActive is true.assertFalse(isActive == false);also passes becauseisActive == falseis false, so assertFalse(false) passes.Final Answer:
Test passes without errors. -> Option AQuick Check:
True condition passes assertTrue and assertFalse(false) passes [OK]
- Thinking assertFalse fails when condition is false
- Confusing assertTrue and assertFalse logic
- Misreading boolean expressions
boolean isReady = false;
assertTrue(isReady);
Solution
Step 1: Analyze the variable value
isReadyis set to false.Step 2: Understand assertTrue behavior
assertTrue(isReady);expects the condition to be true, but it is false, so the assertion fails.Final Answer:
assertTrue fails because isReady is false. -> Option AQuick Check:
assertTrue(false) causes test failure [OK]
- Assuming assertTrue passes with false
- Confusing assertion failure with syntax error
- Not checking variable value before assertion
isEmpty(String s) returns false when the string is not empty. Which assertion correctly tests this?Solution
Step 1: Understand the method behavior
isEmpty("hello")should return false because the string is not empty.Step 2: Choose the assertion that expects false
To verify the method returns false, useassertFalse(isEmpty("hello"));which passes if the method returns false.Final Answer:
assertFalse(isEmpty("hello")); -> Option DQuick Check:
Use assertFalse to check method returns false [OK]
- Using assertTrue expecting false result
- Testing empty string instead of non-empty
- Mixing up assertTrue and assertFalse usage
Ready to prove it?
Pass the test and this pattern moves unknown
