0
0
JUnittesting~20 mins

assertNull and assertNotNull in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit Null Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2: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);
ATest fails because testString is null
BTest passes because testString is null
CTest fails with NullPointerException
DTest passes but warns about null usage
Attempts:
2 left
💡 Hint
assertNull passes only if the object is null.
assertion
intermediate
2:00remaining
Using assertNotNull to check a non-null object
What happens when the following JUnit assertion runs?
JUnit
String testString = "hello";
assertNotNull(testString);
ATest passes but logs a warning
BTest fails because testString is not null
CTest fails with NullPointerException
DTest passes because testString is not null
Attempts:
2 left
💡 Hint
assertNotNull passes only if the object is not null.
Predict Output
advanced
2: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);
ATest fails because number is not null
BTest passes because 5 is a valid Integer
CTest fails with NullPointerException
DTest passes but warns about non-null value
Attempts:
2 left
💡 Hint
assertNull expects the object to be null to pass.
Predict Output
advanced
2: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);
ATest fails because obj is null
BTest passes because obj is declared
CTest throws NullPointerException
DTest passes but logs a warning
Attempts:
2 left
💡 Hint
assertNotNull fails if the object is null.
🧠 Conceptual
expert
2: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?
AassertNotNull because it confirms the object exists
BassertTrue with a null check inside
CassertNull because it confirms the object is null
DassertEquals with an empty string
Attempts:
2 left
💡 Hint
Use assertNull to check if an object is null directly.