Bird
Raised Fist0
JUnittesting~20 mins

assertTrue and assertFalse in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Assert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate
2:00remaining
Using assertTrue with a boolean expression
Which assertion correctly verifies that the variable isActive is true?
JUnit
boolean isActive = (5 > 3);
AassertTrue(isActive);
BassertTrue(isActive == false);
CassertFalse(isActive);
DassertFalse(!isActive);
Attempts:
2 left
💡 Hint
Remember, assertTrue expects the condition to be true.
❓ assertion
intermediate
2:00remaining
Using assertFalse with a boolean expression
Which assertion correctly verifies that the variable isEmpty is false?
JUnit
boolean isEmpty = ("text".isEmpty());
AassertTrue(isEmpty);
BassertFalse(!isEmpty);
CassertFalse(isEmpty);
DassertTrue(!isEmpty);
Attempts:
2 left
💡 Hint
assertFalse expects the condition to be false.
❓ Predict Output
advanced
2:00remaining
What happens when assertTrue fails?
Consider the following JUnit test method. What is the test result when it runs?
JUnit
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

@Test
void testFail() {
    assertTrue(2 + 2 == 5);
}
ATest is skipped automatically.
BTest passes successfully.
CTest fails with a NullPointerException.
DTest fails with an AssertionError.
Attempts:
2 left
💡 Hint
assertTrue expects the condition to be true.
🔧 Debug
advanced
2:00remaining
Identify the incorrect assertion usage
Which option contains an incorrect use of assertFalse that will cause a compilation error?
JUnit
boolean flag = true;
AassertFalse(!flag);
BassertFalse();
CassertFalse(flag);
DassertFalse(flag == false);
Attempts:
2 left
💡 Hint
Check the method parameters required by assertFalse.
🧠 Conceptual
expert
3:00remaining
Choosing between assertTrue and assertFalse for readability
Given a boolean variable isValid, which assertion improves test readability best when checking that isValid is false?
AassertFalse(isValid);
BassertTrue(isValid == false);
CassertTrue(!isValid);
DassertFalse(!isValid);
Attempts:
2 left
💡 Hint
Choose the assertion that directly expresses the expected condition.

Practice

(1/5)
1. What does the JUnit method assertTrue(condition) check in a test?
easy
A. It checks if the condition is false.
B. It checks if the condition is true.
C. It checks if the condition is null.
D. It checks if the condition throws an exception.

Solution

  1. Step 1: Understand the purpose of assertTrue

    The method assertTrue is designed to verify that a given boolean condition is true during a test.
  2. Step 2: Compare with other assertions

    Unlike assertFalse which checks for false, assertTrue specifically expects the condition to be true.
  3. Final Answer:

    It checks if the condition is true. -> Option B
  4. Quick Check:

    assertTrue means condition must be true [OK]
Hint: assertTrue expects true condition, assertFalse expects false [OK]
Common Mistakes:
  • Confusing assertTrue with assertFalse
  • Thinking assertTrue checks for exceptions
  • Assuming assertTrue checks for null
2. Which of the following is the correct syntax to assert that a boolean variable isValid is false in JUnit?
easy
A. assertTrue(isValid);
B. assertFalse(isValid == false);
C. assertFalse(isValid);
D. assertTrue(isValid == true);

Solution

  1. Step 1: Identify the correct assertion method

    To check if isValid is false, we use assertFalse with the variable directly.
  2. 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.
  3. Final Answer:

    assertFalse(isValid); -> Option C
  4. Quick Check:

    Use assertFalse(variable) to check false [OK]
Hint: Use assertFalse(variable) directly to check false [OK]
Common Mistakes:
  • Using assertTrue to check false
  • Adding unnecessary comparisons like == true or == false
  • Confusing assertTrue and assertFalse syntax
3. Consider the following JUnit test code:
boolean isActive = (5 > 3);
assertTrue(isActive);
assertFalse(isActive == false);

What will be the result of running this test?
medium
A. Test passes without errors.
B. Test fails at assertTrue.
C. Test fails at assertFalse.
D. Test throws a compilation error.

Solution

  1. Step 1: Evaluate the boolean expression

    The expression 5 > 3 is true, so isActive is true.
  2. Step 2: Check assertions with isActive = true

    assertTrue(isActive); passes because isActive is true.
    assertFalse(isActive == false); also passes because isActive == false is false, so assertFalse(false) passes.
  3. Final Answer:

    Test passes without errors. -> Option A
  4. Quick Check:

    True condition passes assertTrue and assertFalse(false) passes [OK]
Hint: True variable passes assertTrue and assertFalse(false) [OK]
Common Mistakes:
  • Thinking assertFalse fails when condition is false
  • Confusing assertTrue and assertFalse logic
  • Misreading boolean expressions
4. Identify the error in this JUnit test snippet:
boolean isReady = false;
assertTrue(isReady);
medium
A. assertTrue fails because isReady is false.
B. Variable isReady should be true for assertTrue.
C. Syntax error in assertTrue usage.
D. No error, test will pass.

Solution

  1. Step 1: Analyze the variable value

    isReady is set to false.
  2. Step 2: Understand assertTrue behavior

    assertTrue(isReady); expects the condition to be true, but it is false, so the assertion fails.
  3. Final Answer:

    assertTrue fails because isReady is false. -> Option A
  4. Quick Check:

    assertTrue(false) causes test failure [OK]
Hint: assertTrue fails if condition is false [OK]
Common Mistakes:
  • Assuming assertTrue passes with false
  • Confusing assertion failure with syntax error
  • Not checking variable value before assertion
5. You want to write a JUnit test to verify that a method isEmpty(String s) returns false when the string is not empty. Which assertion correctly tests this?
hard
A. assertFalse(isEmpty(""));
B. assertTrue(isEmpty("hello") == true);
C. assertTrue(isEmpty("hello"));
D. assertFalse(isEmpty("hello"));

Solution

  1. Step 1: Understand the method behavior

    isEmpty("hello") should return false because the string is not empty.
  2. Step 2: Choose the assertion that expects false

    To verify the method returns false, use assertFalse(isEmpty("hello")); which passes if the method returns false.
  3. Final Answer:

    assertFalse(isEmpty("hello")); -> Option D
  4. Quick Check:

    Use assertFalse to check method returns false [OK]
Hint: Use assertFalse to check method returns false [OK]
Common Mistakes:
  • Using assertTrue expecting false result
  • Testing empty string instead of non-empty
  • Mixing up assertTrue and assertFalse usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard