Prev Next

Testing / Mockito Interview Questions

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

Read full answer

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

Read full answer

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

Read full answer

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

Read full answer

5. Can we mock static methods using Mockito?

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

Read full answer

6. Can we mock private methods?

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

Read full answer

7. How do you mock private methods using Mockito?

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

Read full answer

8. What is the use of Mockito.any?

Mockito allows us to create mock objects and stub the behavior for our test cases. We usually mock the behavior using when() and thenReturn() on the mock object.

Read full answer

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

Read full answer

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

Read full answer

11. 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,...

Read full answer

12. Limitations of Mockito.

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

Read full answer

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

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

Read full answer

14. Different ways to create Mock stub in Mockito.

doReturn and thenReturn, doNothing , doThrow and thenThrow.

Read full answer

15. 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();

Read full answer

16. What is spy in Mockito?

Mockito.spy() spies on a real object and invokes the real method. This will allow us to call all the normal methods of the object while still tracking every interaction, just as we would with a mock....

Read full answer

17. What is ArgumentCaptor in Mockito?

Mockito ArgumentCaptor is utilized to capture arguments for mocked methods. ArgumentCaptor is utilized with Mockito verify() methods to get the arguments passed when any method is called.

Read full answer

18. Difference between mock and spy.

Both mock and spy can be used to mock methods or fields. The difference is that in mock, you are creating a complete mock or fake object while in spy, there is the real object and you just spying or...

Read full answer

19. Mention a few Mockito Annotations.

@Mock used to create and inject mocked instances. @Spy is used to create a real object and spy on the real object....

Read full answer

20. What is Hamcrest framework?

Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluable, such as UI validation or data filtering, but it is in the area of writing...

Read full answer

«
»

Comments & Discussions