Challenge - 5 Problems
JUnit Null Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate2:00remaining
Using assertNull to check a null object
Given the following JUnit test snippet, what will be the test result when executed?
JUnit
String testString = null; assertNull(testString);
Attempts:
2 left
💡 Hint
assertNull passes only if the object is null.
✗ Incorrect
assertNull checks if the given object is null. Since testString is null, the assertion passes and the test passes.
❓ assertion
intermediate2:00remaining
Using assertNotNull to check a non-null object
What happens when the following JUnit assertion runs?
JUnit
String testString = "hello";
assertNotNull(testString);Attempts:
2 left
💡 Hint
assertNotNull passes only if the object is not null.
✗ Incorrect
assertNotNull checks that the object is not null. Since testString contains "hello", the assertion passes and the test passes.
❓ Predict Output
advanced2:00remaining
What error occurs with assertNull on a non-null object?
Consider this JUnit test code snippet. What is the test result?
JUnit
Integer number = 5;
assertNull(number);Attempts:
2 left
💡 Hint
assertNull expects the object to be null to pass.
✗ Incorrect
assertNull fails if the object is not null. Since number is 5, the assertion fails and the test fails.
❓ Predict Output
advanced2:00remaining
What happens when assertNotNull is used on a null object?
Analyze this JUnit test snippet and select the correct test result.
JUnit
Object obj = null; assertNotNull(obj);
Attempts:
2 left
💡 Hint
assertNotNull fails if the object is null.
✗ Incorrect
assertNotNull requires the object to be not null. Since obj is null, the assertion fails and the test fails.
🧠 Conceptual
expert2:00remaining
Choosing correct assertion for null checks in JUnit
You want to write a test that confirms a method returns null when no data is found. Which assertion is the best choice?
Attempts:
2 left
💡 Hint
Use assertNull to check if an object is null directly.
✗ Incorrect
assertNull is designed to check if an object is null. It is the clearest and simplest way to confirm a method returns null.