Mock testing is a software testing technique where simulated objects replace real components to test functionality in isolation.
Explanation:
- Mocks simulate real dependencies (like databases, APIs) without actual execution.
- It is widely used in unit testing to ensure that modules work independently.
- Tools like Mockito (Java), unittest.mock (Python), and Moq (C#) are used for mock testing.
Example (Python Mock Test using unittest.mock
)
from unittest.mock import Mock
# Create a mock object
database = Mock()
database.get_user.return_value = {'id': 1, 'name': 'Alice'}
# Test function using the mock
print(database.get_user(1)) # Output: {'id': 1, 'name': 'Alice'}
- Here, we simulate a database call without actually querying a database.
Leave a Reply