Movies (movie)
Products (product)
Services (service)
Solutions (solution)
In unit testing with Java, a stub object is a simplified implementation of a class or an interface that is used to isolate the behavior of the code being tested. Stubs are commonly used to simulate the behavior of external dependencies or components, allowing you to focus on testing specific units of code without worrying about external factors.
Here is an example of how to use a stub object in unit testing:
Suppose you have a UserService
class that relies on a UserRepository
interface to fetch user details.
// Interface
public interface UserRepository {
User findUserById(String id);
}
// Implementation of Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public String getUserName(String id) {
User user = userRepository.findUserById(id);
return user != null ? user.getName() : "User not found";
}
}
// Model Class
public class User {
private final String id;
private final String name;
public User(String id, String name) {
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
}
UserRepository
:You can create a stub for UserRepository
in your test class.
// Stub Implementation
public class UserRepositoryStub implements UserRepository {
@Override
public User findUserById(String id) {
if ("123".equals(id)) {
return new User("123", "Alice");
}
return null;
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class UserServiceTest {
@Test
public void testGetUserName() {
// Arrange: Use the stub
UserRepository userRepositoryStub = new UserRepositoryStub();
UserService userService = new UserService(userRepositoryStub);
// Act: Call the method
String userName = userService.getUserName("123");
// Assert: Verify the result
assertEquals("Alice", userName);
// Test for a non-existent user
assertEquals("User not found", userService.getUserName("999"));
}
}
Would you like an example using Mockito for dynamic stubbing?
By MOUSTAFA ALSAYEH