The assertNotNull method in JUnit is used to assert that an object is not null. JUnit provides several overloaded versions of this method to handle different scenarios and to provide custom messages for test failures. This guide covers the basics of using the assertNotNull method, including its syntax and examples of its different overloads.
Table of Contents
- Introduction
assertNotNullMethod Syntax- Examples
- Basic Usage
- Using a Custom Message
- Using a Message Supplier
- Real-World Use Case
- Conclusion
Introduction
The assertNotNull method in JUnit is an assertion method used to verify that a given object is not null. If the object is null, the assertion fails, and the test is marked as failed. This method is fundamental for writing unit tests that check for the presence of objects in your code.
assertNotNull Method Syntax
Here is the basic syntax of the assertNotNull method with its different overloads:
// Asserts that the supplied actual value is not null
static void assertNotNull(Object actual);
// Asserts that the supplied actual value is not null with a custom message
static void assertNotNull(Object actual, String message);
// Asserts that the supplied actual value is not null with a custom message supplier
static void assertNotNull(Object actual, Supplier<String> messageSupplier);
Parameters:
actual: The actual object to be checked.message: Optional. A custom message to display if the assertion fails.messageSupplier: Optional. A supplier that provides a custom message to display if the assertion fails.
Returns:
- Nothing. The method throws an assertion error if the object is null.
Examples
Basic Usage
Verify that an object is not null.
Example
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
public class NotNullTest {
@Test
void testNotNull() {
Object actualValue = "Hello, JUnit!";
assertNotNull(actualValue);
}
}
Using a Custom Message
Include a custom message to display if the assertion fails.
Example
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
public class CustomMessageTest {
@Test
void testNotNullWithMessage() {
Object actualValue = "Hello, JUnit!";
assertNotNull(actualValue, "The object should not be null");
}
}
Using a Message Supplier
Use a message supplier to lazily generate a custom message if the assertion fails.
Example
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
import java.util.function.Supplier;
public class MessageSupplierTest {
@Test
void testNotNullWithMessageSupplier() {
Object actualValue = "Hello, JUnit!";
Supplier<String> messageSupplier = () -> "The object should not be null";
assertNotNull(actualValue, messageSupplier);
}
}
Real-World Use Case
Testing a UserService Class
A common use case for assertNotNull is testing methods that return objects in a UserService class. For example, verifying that the method returns a valid user object.
Class Under Test – UserService
class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class UserService {
public User getUserById(int id) {
// Simulate a user retrieval operation
if (id == 1) {
return new User("John Doe");
} else {
return null;
}
}
}
Test Class – UserServiceTest
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
public class UserServiceTest {
private final UserService userService = new UserService();
@Test
void testGetUserByIdNotNull() {
User user = userService.getUserById(1);
assertNotNull(user, "The user should not be null");
}
@Test
void testGetUserByIdNotNullWithMessageSupplier() {
User user = userService.getUserById(1);
Supplier<String> messageSupplier = () -> "The user should not be null";
assertNotNull(user, messageSupplier);
}
}
In this example, the UserServiceTest class tests the getUserById method of the UserService class using assertNotNull. It includes tests to ensure that the method returns a valid user object when a valid ID is provided.
Conclusion
The assertNotNull method in JUnit is used for verifying that a given object is not null in your tests. By using assertNotNull and its various overloads, you can ensure that your tests provide clear feedback when null values are unexpectedly encountered. Understanding and using the assertNotNull method effectively is crucial for developing robust and maintainable Java applications.