0
0
JUnittesting~20 mins

assertTrue and assertFalse in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 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.