Mockito Interview Questions

Mockito Interview Questions

1) What is mockito?

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.

2) List some benefits of Mockito?

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.

3) What is mocking in testing?

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.

4) What is difference between Assert and Verify in mockito?

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.

5) List some limitations of Mockito?

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.

6) What is use of mock() method in Mockito?

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.

7) What is ArgumentCaptor in Mockito?

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.

8) What is Hamcrest ?

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.

9) List some Mockito Annotations?

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.

10) What is PowerMock?

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.

11) What is EasyMock?

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.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.