The assertSame method in JUnit is used to assert that two objects are the same instance. 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 assertSame method, including its syntax and examples of its different overloads.
Table of Contents
- Introduction
assertSameMethod Syntax- Examples
- Basic Usage
- Using a Custom Message
- Using a Message Supplier
- Real-World Use Case
- Conclusion
Introduction
The assertSame method in JUnit is an assertion method used to verify that two given objects are the same instance. If the objects are not the same instance, the assertion fails, and the test is marked as failed. This method is fundamental for writing unit tests that ensure certain objects are identical.
assertSame Method Syntax
Here is the basic syntax of the assertSame method with its different overloads:
// Asserts that the supplied objects are the same instance
static void assertSame(Object expected, Object actual);
// Asserts that the supplied objects are the same instance with a custom message
static void assertSame(Object expected, Object actual, String message);
// Asserts that the supplied objects are the same instance with a custom message supplier
static void assertSame(Object expected, Object actual, Supplier<String> messageSupplier);
Parameters:
expected: The expected object instance.actual: The actual object instance produced by the code.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 objects are not the same instance.
Examples
Basic Usage
Verify that two objects are the same instance.
Example
import static org.junit.jupiter.api.Assertions.assertSame;
import org.junit.jupiter.api.Test;
public class SameTest {
@Test
void testSame() {
Object obj1 = new Object();
Object obj2 = obj1;
assertSame(obj1, obj2);
}
}
Using a Custom Message
Include a custom message to display if the assertion fails.
Example
import static org.junit.jupiter.api.Assertions.assertSame;
import org.junit.jupiter.api.Test;
public class CustomMessageTest {
@Test
void testSameWithMessage() {
Object obj1 = new Object();
Object obj2 = obj1;
assertSame(obj1, obj2, "The objects should be the same instance");
}
}
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.assertSame;
import org.junit.jupiter.api.Test;
import java.util.function.Supplier;
public class MessageSupplierTest {
@Test
void testSameWithMessageSupplier() {
Object obj1 = new Object();
Object obj2 = obj1;
Supplier<String> messageSupplier = () -> "The objects should be the same instance";
assertSame(obj1, obj2, messageSupplier);
}
}
Real-World Use Case
Testing a UserService Class
A common use case for assertSame is testing methods that should return the same object instance in a UserService class. For example, verifying that a singleton service returns the same user instance for a given ID.
Class Under Test – UserService
class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class UserService {
private final User user = new User("John Doe");
public User getUser() {
return user;
}
}
Test Class – UserServiceTest
import static org.junit.jupiter.api.Assertions.assertSame;
import org.junit.jupiter.api.Test;
public class UserServiceTest {
private final UserService userService = new UserService();
@Test
void testGetUserSameInstance() {
User user1 = userService.getUser();
User user2 = userService.getUser();
assertSame(user1, user2, "The user instances should be the same");
}
@Test
void testGetUserSameInstanceWithMessageSupplier() {
User user1 = userService.getUser();
User user2 = userService.getUser();
Supplier<String> messageSupplier = () -> "The user instances should be the same";
assertSame(user1, user2, messageSupplier);
}
}
In this example, the UserServiceTest class tests the getUser method of the UserService class using assertSame. It includes tests to ensure that the method returns the same user instance each time it is called.
Conclusion
The assertSame method in JUnit is used for verifying that two given objects are the same instance in your tests. By using assertSame and its various overloads, you can ensure that your tests provide clear feedback when objects are unexpectedly different instances. Understanding and using the assertSame method effectively is crucial for developing robust and maintainable Java applications.