0
0
JUnittesting~5 mins

assertNull and assertNotNull in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does assertNull check in a JUnit test?
assertNull checks that the object being tested is null. If the object is not null, the test fails.
Click to reveal answer
beginner
What is the purpose of assertNotNull in JUnit?
assertNotNull verifies that the object is not null. If the object is null, the test fails.
Click to reveal answer
beginner
Write a simple example of assertNull usage in JUnit.
Example:<br>
String result = null;
assertNull(result);
This test passes because result is null.
Click to reveal answer
beginner
Write a simple example of assertNotNull usage in JUnit.
Example:<br>
String result = "Hello";
assertNotNull(result);
This test passes because result is not null.
Click to reveal answer
intermediate
Why is it important to use assertNull and assertNotNull in tests?
They help confirm that objects are in the expected state (null or not null), which prevents errors like NullPointerException in real use.
Click to reveal answer
What happens if assertNull(object) is used but object is not null?
AThe test fails
BThe test passes
CThe test is skipped
DThe test throws an exception
Which assertion would you use to check that a method returns a valid object and not null?
AassertTrue
BassertEquals
CassertNull
DassertNotNull
In JUnit, what is the return type of assertNull and assertNotNull methods?
Aboolean
BObject
Cvoid
Dint
Which of these is a correct way to use assertNotNull?
AassertNotNull("text");
BassertNotNull(null);
CassertNotNull();
DassertNotNull(5);
What message can you add to assertNull to help understand test failures?
AassertNull(object, "Object should be null");
BassertNull("Object should be null", object);
CassertNull(object); // Object should be null
DassertNull(object, null);
Explain in your own words when and why you would use assertNull and assertNotNull in a test.
Think about checking if something exists or not in your code.
You got /4 concepts.
    Describe a simple test scenario where assertNull would be useful.
    Imagine a method that returns null if no user is found.
    You got /3 concepts.