0% found this document useful (0 votes)
2 views

Mockito

Uploaded by

Abs Wps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Mockito

Uploaded by

Abs Wps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

MOCK: Objects that tracks the interactions of the objects and verifies the usage i.

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?

o Difference between mocking, stubbing, and spying.

o Understanding the importance of unit testing and how Mockito


facilitates it.
2. Setting Up Mockito
o Adding Mockito as a dependency (Maven, Gradle).

o Basic annotations: @Mock, @InjectMocks, @Spy, @Captor,


@RunWith(MockitoJUnitRunner.class).
o Setting up a Mockito test class.

3. Creating Mocks
o Creating mock objects using Mockito.mock(Class.class).

o Using @Mock annotation to create mock objects.


o Understanding the behavior of mocks (returning default values).

4. Stubbing Methods
o Stubbing methods with when(...).thenReturn(...).

o Stubbing void methods with doNothing(), doThrow(), etc.

o Using thenThrow(...) for exceptions.

o Chaining method calls.

5. Verifying Interactions
o Verifying that methods were called using verify(mock).method(...).

o Checking call count: verify(mock, times(n)), verify(mock, never()).

o Verifying the order of method calls with InOrder.

6. Argument Captors
o Using ArgumentCaptor to capture method arguments passed to mocks.

o Example use cases for argument capturing.

7. Spying on Real Objects


o Creating spies with Mockito.spy(realObject).

o Differences between mocks and spies.

o Partial mocking: when to use a spy versus a mock.

8. Handling Exceptions
o Throwing exceptions from mocked methods.

o Verifying that exceptions were thrown during interactions.

9. Using Matchers
o Understanding matchers like any(), eq(), isA(), etc.

o Combining matchers in verifications and stubbings.

10.Mocking Static Methods


o Using the mockito-inline extension for mocking static methods.

o Understanding limitations and use cases.

11.Mocking Constructors
o Using Mockito with PowerMockito for mocking constructors.

o Limitations of mocking static and final methods.


12.Best Practices
o Keeping tests isolated and independent.

o Avoiding over-mocking (not mocking too many dependencies).

o Writing clear and maintainable test cases.

o Use of clear naming conventions for tests.

13.Integration with Other Testing Frameworks


o Using Mockito with JUnit 4 and JUnit 5.

o Combining Mockito with Spring Test for integration testing.

14.Common Pitfalls
o Not resetting mocks if necessary.

o Overusing @InjectMocks.

o Misunderstanding the behavior of mocks vs. spies.

You might also like