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?✗ Incorrect
assertNull expects the object to be null. If it is not, the test fails.Which assertion would you use to check that a method returns a valid object and not null?
✗ Incorrect
assertNotNull confirms the object is not null, meaning the method returned a valid object.In JUnit, what is the return type of
assertNull and assertNotNull methods?✗ Incorrect
Both
assertNull and assertNotNull are void methods that throw exceptions if the assertion fails.Which of these is a correct way to use
assertNotNull?✗ Incorrect
You must pass a non-null object to
assertNotNull. "text" is a non-null String.What message can you add to
assertNull to help understand test failures?✗ Incorrect
In JUnit, the message comes first:
assertNull(String message, Object object).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.