🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Mockito, mocks and stubs are used to simulate the behavior of real objects in unit tests. While both are used to isolate the code under test, they serve different purposes and have distinct characteristics. This article will explain the differences between mocks and stubs in Mockito and provide a summary table for quick reference.
Mocks
A mock is a test double that simulates the behavior of a real object. Mocks are used to verify interactions between the tested object and its dependencies. In Mockito, mocks are created using the @Mock annotation or the mock() method.
Example
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import java.util.List;
@ExtendWith(MockitoExtension.class)
public class MockExampleTest {
@Mock
private List<String> mockList;
@Test
public void testMock() {
mockList.add("test");
verify(mockList).add("test");
}
}
Key Points:
- Mocks are used to verify interactions.
- Mocks can throw exceptions or return predefined values.
Stubs
A stub is a test double that provides predetermined responses to method calls. Stubs are primarily used to set up the environment for the test by providing specific return values. In Mockito, stubs are created using the when() method.
Example
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.extension.ExtendWith;
interface Service {
String getData();
}
@ExtendWith(MockitoExtension.class)
public class StubExampleTest {
@Test
public void testStub() {
Service stubService = mock(Service.class);
when(stubService.getData()).thenReturn("stubbed data");
String result = stubService.getData();
assertEquals("stubbed data", result);
}
}
Key Points:
- Stubs provide predefined responses.
- Stubs are primarily used to set up test data.
Differences Between Mock and Stub
| Feature | Mock | Stub |
|---|---|---|
| Purpose | Verify interactions | Provide predefined responses |
| Creation | @Mock annotation or mock() method |
mock() method + when() method |
| Behavior | Simulates behavior, can throw exceptions | Returns specific values, no exceptions |
| Verification | Yes, verifies method calls | No, used only to return values |
| Focus | Interaction testing | State-based testing |
| Typical Use Case | Ensuring correct method calls | Setting up test environment |
Conclusion
Mocks and stubs are essential concepts in unit testing with Mockito. Understanding their differences and appropriate use cases helps you write more effective and maintainable tests. Mocks focus on interaction testing, verifying that methods are called with the expected arguments. Stubs, on the other hand, are used to set up the test environment by providing specific return values. By leveraging both mocks and stubs, you can thoroughly test your code's behavior and interactions.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment