Mockito
Mockito
e
verifies if the object was called and returned the required response.
STUB: Hardcoded response to method calls. Whenever a method is called a specific
response is returned. The response is overrides the actual response of the method.
We need to create new stub every time.
DUMMY: It is a fake object which is never used. It is just created to complete the
signature or at places where the object is only passed but not utilized.
FAKE: It is a completely fake object which does not have anything same as the
original object.
Fake DB to test, or testing external dependencies
SPY: It is used to track the interaction of the objects and where it was utilized.
Allows real method calls
MOCKITO
1. powerful mocking framework for Java that allows developers to create mock
objects for unit testing.
2. Allows Mocking-> Stubbing -> Verification -> argument capturing
3. Need to setup using Mockito.mock()
4. For stubbing when, thenreturn is used on mock object
5. For verification verify is used
Mockito
1. Introduction to Mockito
o What is Mockito and its purpose in testing?
3. Creating Mocks
o Creating mock objects using Mockito.mock(Class.class).
4. Stubbing Methods
o Stubbing methods with when(...).thenReturn(...).
5. Verifying Interactions
o Verifying that methods were called using verify(mock).method(...).
6. Argument Captors
o Using ArgumentCaptor to capture method arguments passed to mocks.
8. Handling Exceptions
o Throwing exceptions from mocked methods.
9. Using Matchers
o Understanding matchers like any(), eq(), isA(), etc.
11.Mocking Constructors
o Using Mockito with PowerMockito for mocking constructors.
14.Common Pitfalls
o Not resetting mocks if necessary.
o Overusing @InjectMocks.