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
Step 1: Identify the correct assertion method
To check if isValid is false, we use assertFalse with 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 C
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
The expression 5 > 3 is true, so isActive is true.
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.
Final Answer:
Test passes without errors. -> Option A
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
Step 1: Analyze the variable value
isReady is 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 A
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
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, use assertFalse(isEmpty("hello")); which passes if the method returns false.
Final Answer:
assertFalse(isEmpty("hello")); -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.