Challenge - 5 Problems
Assert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate2:00remaining
Using assertTrue with a boolean expression
Which assertion correctly verifies that the variable
isActive is true?JUnit
boolean isActive = (5 > 3);
Attempts:
2 left
💡 Hint
Remember, assertTrue expects the condition to be true.
✗ Incorrect
assertTrue(isActive); passes because isActive is true. Other options either check for false or negate the condition incorrectly.
❓ assertion
intermediate2:00remaining
Using assertFalse with a boolean expression
Which assertion correctly verifies that the variable
isEmpty is false?JUnit
boolean isEmpty = ("text".isEmpty());Attempts:
2 left
💡 Hint
assertFalse expects the condition to be false.
✗ Incorrect
assertFalse(isEmpty); passes because isEmpty is false. Option C is logically correct but uses assertTrue, not assertFalse.
❓ Predict Output
advanced2: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); }
Attempts:
2 left
💡 Hint
assertTrue expects the condition to be true.
✗ Incorrect
The condition 2 + 2 == 5 is false, so assertTrue throws an AssertionError causing the test to fail.
🔧 Debug
advanced2:00remaining
Identify the incorrect assertion usage
Which option contains an incorrect use of assertFalse that will cause a compilation error?
JUnit
boolean flag = true;
Attempts:
2 left
💡 Hint
Check the method parameters required by assertFalse.
✗ Incorrect
assertFalse() without any argument causes a compilation error because it requires a boolean parameter.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Choose the assertion that directly expresses the expected condition.
✗ Incorrect
assertFalse(isValid); clearly states the expectation that isValid should be false, improving readability over negations.