Mockito Junit Interview Question and Answer for fresher and experienced with example | Code Decode

In this video of code decode we have explained mockito interview questions and answers

What is Mockito in JUnit?
Mockito is a Java-based mocking framework used for unit testing of Java application. Its used in conjunction with other testing frameworks such as JUnit and TestNG. It internally uses Java Reflection API in order to create mock objects for a given interface. Mock objects are nothing but proxy for actual implementations.

What is Mocking?

Mocking is a process of developing the objects that act as the mock or clone of the real objects. In other words, mocking is a testing technique where mock objects are used instead of real objects for testing purposes. Mock objects provide a specific (dummy) output for a particular (dummy) input passed to it.

Mocking for unit testing is when you create an object that implements the behavior of a real subsystem in controlled ways. In short, mocks are used as a replacement for a dependency.

To mock objects, you need to understand these key concepts of mocking, i.e., stub, fake, spy, dummy and mock. Some of the unit tests involve only stubs, whereas some involve fake, and mocks.

A method invoked using mocked reference does not execute the actual method body defined in the class file, rather the method behavior is configured using when(...).thenReturn(...) methods.

What is @Mock and @InjectMocks?

In mockito-based junit tests, @Mock annotation creates mocks and @InjectMocks creates actual objects and injects mocked dependencies into it.

We must define the when(...).thenReturn(...) methods for mock objects whose class methods will be invoked during actual test execution.

So In a junit test, we create objects for the class which need to be tested and its methods to be invoked using inject mock

We create mocks for the dependencies which will not be present in the test environment and objects are dependent on it to complete the method call.

Most Asked Core Java Interview Questions and Answers :

Advance Java Interview Questions and Answers :

Java 8 Interview Questions and Answers :

Hibernate Interview Questions and Answers :

Spring Boot Interview Questions and Answers :

Angular Playlist :

SQL Playlist :


Subscriber and Follow Code Decode
Subscriber Code Decode :
LinkedIn :
Instagram :

#mockito #codedecode #junit #javainterviewquestion
Be the first to comment