Most frequently asked Mockito Interview Questions
- What is Mockito?List some of its advantages?
- Explain Mockito Framework?
- What is Hamcrest ?
- How to mock void methods with Mockito?
- Difference between @Mock and @InjectMocks?
- Why do we need mocking?
- What Are Junits ?
- What is the use of Mockito.any?
- What is Spying in Mockito?
- Why is mockito so simple?
- What are the limitations of Mockito?
- What is EasyMock?
What is Mockito?List some of its advantages?
Mockito is an open source testing framework for Java released under the MIT License. The framework allows the creation of test double objects in automated unit tests for the purpose of test-driven development or behavior-driven development.Mockito allows developers to verify the behavior of the system under test (SUT) without establishing expectations beforehand. One of the criticisms of mock objects is that there is a tight coupling of the test code to the system under test, mockito attempts to eliminate the expect-run-verify pattern by removing the specification of expectations. Mockito also provides some annotations for reducing boilerplate code.Advantages of Mockito are:
- Renaming interface method names or reordering parameters will not break the test code as Mocks are created at runtime.
- Supports return values.
- Supports exceptions.
- Supports check on order of method calls.
- Supports creating mocks using annotation.
Explain Mockito Framework?
In Mockito, you always check a particular class. The dependency in that class is injected using mock object. So, for example, if you have service class, then the Dao class are injected as a mockDao. This enables us to check only the method of that given service class and whether they are performing as expected or not.
What is Hamcrest ?
Hamcrest is a popular framework that help us to create the matcher objects. It is used for writing software tests and also performs unit testing in Java programming language. Hamcrest is mainly used with other unit testing frameworks like JUnit, jMockit, Mockito.It supports creating customized assertion matchers allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.How to mock void methods with Mockito?
As with other articles focused on the Mockito framework (such as Mockito Verify, Mockito When/Then, and Mockito's Mock Methods), the MyList class shown below will be used as the collaborator in test cases.public class List extends AbstractList{ @Override public void add(int index, String element) { // no-op } }
Difference between @Mock and @InjectMocks?
@Mock annotation creates mocks.@InjectMocks creates class objects. Use @InjectMocks when actual method body needs to be executed for a given class.
Why do we need mocking?
Mocking is generally required when :- The component under test has dependencies that are not yet implemented or the implementation is in progress.
- A good example can be a REST API endpoint which will be available later at some point in time, but you have consumed it in the code via a dependency.
- Now as the real implementation is still not available, you really know most of the time what is the expected response of that API. Mocks allow you to test those kinds of integration.
- Component updates the state in the system.
What Are Junits ?
JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated with SUnit.JUnit is linked as a JAR at compile-time.
JUnit developed the idea of first testing and then coding that assure setting up of the test data and defining the expected output and then coding. This procedure increases productivity and stability of program code and reduce the time for debugging.
What is the use of Mockito.any?
Mockito allows us to create mock objects and stub the behavior for our test cases. We usually mock the behavior using when() and thenReturn() on the mock object.What is Spying in Mockito?
Mockito provides option to create spy on real objects. When spy is called, then actual method of real object is called.SYNTAX:
//create a spy on actual object calcService = spy(calculator); //perform operation on real object //test the add functionality Assert.assertEquals(mathApplication.add(30.0, 10.0),40.0,0);
Why is mockito so simple?
To promote simple test code that hopefully pushes the developer to write simple and clean application code. I wrote this paragraph long before version 1.5. Mockito is still quite lean but the number of features increased because many users found valid cases for them.What are the limitations of Mockito?
Mockito 2.x specific limitations:Requires Java 6+
Cannot mock static methods
Cannot mock constructors
Cannot mock equals(), hashCode(). Firstly, you should not mock those
methods. Secondly, Mockito defines and depends upon a specific implementation of these methods. Redefining them might break Mockito.
Mocking is only possible on VMs that are supported by Objenesis. Don't worry, most VMs should work just fine.
Cannot mock final classes
Cannot mock final methods - their real behavior is executed without any exception. Mockito cannot warn you about mocking final methods so be vigilant.
Cannot mock static methods
Cannot mock constructors