What is mocking?
Mocking is a test of the functionality of a class which is in isolation. Mocking does not needed a database connection or file server read test. The object of this mock will do mocking for actual service. Its object returns as parameter a dummy data to dummy input passed to it.
Mockito
Mockito is useful to create mock objects. It has java reflection to create mock objects for a given interface. Mock objects are proxy for actual implementation.
For example, consider a case of stock service which returns the price details of a stock. During the development, the actual stock service cannot be used to get real-time data. Mockito does the same easily.
The benefits of Mockito are
- No Handwriting- we don’t have to write mock objects on your own.
- Mock are created at execution time
- Return value support- supports return values.
- Exception support-It has support exceptions.
- Order check support-It supports check on order of method calls.
- Annotation support-It has supports creating mock using annotation.
The brief description of mocking
- Stub-Stub objects are defined data providing answer the calls during testing. They will be as dummy objects, very minimal number of methods required for a test. It also has methods to verify other methods used to access the internal state of stub, when necessary. Stub object is generally used for state verification.
- Fake- Fake and False objects contains working implementations but are different from the production. It takes shortcuts and also contains the simplified version of the production code.
- Mock- Mock objects act as dummy or clone of real object in testing. They are generally created by an open-source library or a mocking framework like Mockito, EasyMock etc. Mock objects are typically used for behavior verification.
Need for Mocking
There are reasons for using mocking, which are as follows
- If we want to test a component that depends on the other component, but it is under development. It generally uses when working in a team and parts are categorized among many team members. As a case, mocking plays an essential role in the testing of that component. If mocking, is not done we should to wait for the completion of the required elements for testing.
- If the real components perform slow operations while dealing with database connection or another complex read/write operation. Sometimes the database queries can take 10, 20 or more seconds to run. We require mock object to perform testing and it can be done via mocking.
- If there is an infrastructure concern that will make the testing impossible. It is similar to the first case.
Mockito Mock () Method
This method creates small objects of mock tool for class. Mockito contains five mock () methods with various arguments. When we dont assign to mocks, they set back default values. The five methods have similar function of mocking the object. There are many methods with different parameters.
- Mock() method with class:
Syntax: <T> mock(Class<T> classToMock, MockSettings mockSettings)
- Mock()method with ReturnValues:This is for creation of mock objects of a given class or given class or interface.
Syntax: <T> mock(Class<T> classToMock, ReturnValues returnValues).
- Mock() method with string-
Syntax: <T> mock(Class<T> classToMock, String name)
Mockito verify () method
The verify method will be used to check particular methods that are not called. It validates the behaviour which happened once in a test. It will be used at the bottom of testing code to assure that will be defined methods are called. Mockito framework will keep track of every method that calls the parameter for mocking objects.once the mocking is over we can verify that will be defined conditions which are met or may not busy by verify() method. This type of testing is sometimes known behavioural testing.
There are two methods of verify
1. verify() method: It will verify behavior happened once
syntax:<T>verify<T mock>
2.verify() method verification mode: This checks behavior happened at least once many number of times
syntax<T> verify(T mock,verificationMode mode)
Questions
1. What is Mockito?
2. Explain the working of Mockito?