Mockito Interview Questions

Mockito Interview Questions
Download Mockito Interview Questions PDF

Below are the list of Best Mockito Interview Questions and Answers

Mockito is a JAVA-based library used for unit testing applications. This open-source library plays an important role in automated unit tests for the purpose of test-driven development or behavior-driven development. It uses a mock interface to add dummy functionality in the unit testing. It also uses Java reflection to create mock objects for an interface to test it.

Some of the benefits of Mockito are,

  • It has support for return values.
  • It supports exceptions.
  • It has support for the creation of mock using annotation.
  • There is no need to write mock objects on your own with Mockito.
  • The test code is not broken when renaming interface method names or reordering parameters.

Mocking in Mockito is the way to test the functionality of the class. The mock objects do the mocking process of real service in isolation. These mock objects return the dummy data corresponding to the dummy input passed to the function. The mocking process does not require you to connect to the database or file server to test functionality.

Both statements are used to add validations to the test methods in the test suites but they differ in the following.

The Assert command is used to validate critical functionality. If this validation fails, then the execution of that test method is stopped and marked as failed.

In the case of Verify command, the test method continues the execution even after the failure of an assertion statement. The test method will be marked as failed but the execution of remaining statements of the test method is executed normally.

Some limitations of the mockito are,

  • It cannot mock constructors or static methods.
  • It requires Java version 6 plus to run.
  • It also cannot mock equals(), hashCode() methods.
  • VM mocking is only possible on VMs that are supported by Objenesis.

The Mock() method is used to create and inject the mocked instances. It gives you boilerplate assignments to work with these instances. The other way of creating the instances is using the @mock annotations.

ArgumentCaptor is a class that is used to capture the argument values for future assertions. This class is defined in the org.mockito package and can be imported from it.

Some of the methods present in this class are

  • capture(),
  • getValue(),
  • getAllValues(), and ArgumentCaptor <U> forClass.

Hamcrest is a framework used for writing customized assertion matchers in the Java programming language. It allows the match rules to be defined declaratively. This makes the hamcrest valuable in UI validation, data filtering, writing flexible tests, etc. It can also be used with mock objects by using adaptors. Hamcrest can also e used with JUnit and TestNG.

Some of the Mockito annotations are,

  • @Mock - It is used to create and inject mocked instances.
  • @Spy - It is used to create a real object and spy on the real object.
  • @Captor - It is used to create an ArgumentCaptor.
  • @InjectMocks - It is used to create an object of a class and insert its dependencies.

PowerMock is a Java framework for unit testing purposes. This framework extends from other mock libraries with more powerful capabilities. It uses custom classloader and bytecode manipulation for mocking the static methods, constructors, final classes, private methods, and more. It normally lets you test the code that is regarded as untestable.

EasyMock is a framework for creating mock objects as it uses Java reflection to create it for a given interface. It relieves the user of hand-writing mock objects as it uses a dynamic mock object generator.

Some other perks you get with EasyMock are

  • exception support,
  • return value support,
  • refactoring scale,
  • annotation support and order check support.