Mockito Interview Questions
Mockito Interview Questions
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.
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.
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.
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.
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.
Q11. 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.
exception support,
return value support,
refactoring scale,
annotation support and order check support.