Prev Next

JUnit / Mockito Interview Questions

Could not find what you were looking for? send us the question and we would be happy to answer your question.

What is Mockito framework?

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.

Mockito is a mocking framework.

What is meant by mocking in unit testing?

Mocking is the way to test the functionality of the class. The mock objects perform 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.

Explain 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.

Why do we need mocking in the Unit testing?

There are many use cases of mocking needed to perform unit testing of the code under isolation and make the test highly repeatable and predictable.

Mocking is generally required when,

  • Component shouldn't update the state in the system during unit testing. For example, saving data to the database will alter the state, so during unit testing, DB calls could be mocked in the component under test.
  • When the implementation of certain API/libraries/dependency is unavailable, we can mock those to pretend that it is available. For instance, assume we are consuming a REST API that provides a list of stock. While the REST API implementation is in progress, we can simply mock the REST API so we get the list of sample stocks.
Can we mock static methods using Mockito?

No, Use PowerMockito on top of Mockito to mock static methods.

Can we mock private methods?

No, not using mockito. However, using PowerMockit we can mock private methods.

How do you mock private methods using Mockito?

Declare the methods as protected and mark them with the annotation @visibleForTesting.

Difference between Assert and Verify?

Assert works only if assertions (-ea) are enabled which is not required for Verify. Assert throws an exception and hence doesn't continue with the test if assert evaluates to false whereas it is not so with Verify.

Difference between doReturn and thenReturn.

No type safety: The parameter of doReturn is Object unlike thenReturn. So, there is no type checking in the compile time. When the type is mismatched in the runtime, there would be an WrongTypeOfReturnValue execption.

No side effect: The advantage of doReturn-when is no side effect. You can use doReturn-when to specify a return value on a spied object without making a side effect.

Why static methods cannot be mocked using Mockito?

Mock libraries typically create mocks by dynamic instance creation at runtime, either through interfaces or through inheritance, and as the static method is not associated with any particular instance it is not possible for mocking frameworks (like Mockito, easy mock, etc) to mock Static methods.

Frameworks like PowerMock do have support for static methods that perform bytecode manipulation at runtime in order to mock static methods.

Limitations of Mockito.

Mockito is a framework of choice for most of the java based projects. It is easy to implement, read and understand.

Limitations: static and private methods, constructors, final class cannot be mocked.

Can we mock Interface default methods (Java 8) using Mockito?

From Mockito 2 onwards, Interface default methods can be mocked.

Different ways to create Mock stub in Mockito.

  • doReturn and thenReturn,
  • doNothing ,
  • doThrow and thenThrow.
How do you create a stub for a method returning void?

Void methods can be used with Mockito's doNothing(), doThrow(), and doAnswer() methods.

doNothing().when(someObject).someMethod(anyObject());

Mockito.doThrow(new Exception()).when(instance).methodName();
«
»
TOSCA

Comments & Discussions