Interview

20 Mockito JUnit Interview Questions and Answers

Prepare for the types of questions you are likely to be asked when interviewing for a position where Mockito JUnit will be used.

Mockito is a popular Java testing framework that allows developers to create mock objects for testing purposes. If you’re applying for a Java development position, it’s likely that you’ll be asked about Mockito during the interview process. To help you prepare, we’ve compiled a list of common Mockito questions and provided sample answers to give you an idea of what the interviewer is looking for.

Mockito JUnit Interview Questions and Answers

Here are 20 commonly asked Mockito JUnit interview questions and answers to prepare you for your interview:

1. What is Mockito?

Mockito is a Java library that is used for mocking objects in tests. This allows you to test your code without having to actually create the objects that you are testing.

2. What are the main benefits of using Mockito Framework?

The benefits of using Mockito Framework are that it allows for more flexibility and customizability in unit testing, as well as providing an easy way to mock objects for testing purposes. Additionally, Mockito can be used to verify that certain methods are being called on objects during testing, which can be helpful in identifying potential issues.

3. Can you explain what a mock object is?

A mock object is a simulated object that mimics the behavior of a real object in a controlled way. Mock objects are often used in unit testing, as they can help to isolate the code being tested and allow for more targeted testing.

4. Why do we need to use mocks when testing our code?

Mocks allow us to test our code in isolation, without depending on the behavior of other classes or modules. This is especially important when those other classes or modules are out of our control, such as when we are testing code that interacts with a third-party API. By using mocks, we can ensure that our code is behaving as expected, without being affected by changes in the behavior of the API.

5. What’s the difference between a stub, a fake and a mock?

A stub is an object that returns canned responses to method calls. A fake is an object that looks and behaves like the real thing, but is actually a fake. A mock is an object that can record method calls and replay them later.

6. How can you create a mock object using Mockito framework?

You can create a mock object using the Mockito framework by using the Mockito.mock() method. This method takes a Class object as a parameter, and returns an instance of that class that is a mock object.

7. How do you verify a method was called with certain parameters on a mock object?

You can use the Mockito verify() method to verify that a method was called with certain parameters on a mock object. For example, you could use verify() to verify that a mock object’s save() method was called with the correct data object as a parameter.

8. How do you set up interactions between test classes and mock objects?

In Mockito, you can use the @Mock annotation to create mock objects. These mock objects can then be injected into your test classes using the @InjectMocks annotation. You can then use Mockito’s verify() method to verify that interactions between your test classes and mock objects are happening as expected.

9. What are matchers in Mockito?

Matchers in Mockito are used to provide flexible verification or stubbing. With matchers, you can specify any arguments that you want, and Mockito will make sure that those arguments are used.

10. Is it possible to create mock objects for final classes or methods? If yes, then how?

Yes, it is possible to create mock objects for final classes and methods. This can be done using the PowerMockito library.

11. Can you give me some examples where Mocking is useful?

Mocking is useful in a variety of situations, but is most commonly used in unit testing. When unit testing, it is often difficult or impossible to test a particular piece of code in isolation. Mocking allows you to create “fake” objects that stand in for the real objects in your code, and can be used to test how your code interacts with those objects. This is especially useful when testing code that interacts with databases or other external services, as it allows you to test your code without actually having to set up and use those services.

12. How do you verify the number of times a method gets executed on a mock object?

You can verify the number of times a method gets executed on a mock object using the Mockito.verify() method.

13. What are all the different ways that a mock object can be verified?

There are a few different ways that a mock object can be verified. The most common way is to simply call the verify() method on the mock object, passing in the method that you want to verify was called. Another way is to use the @Verify annotation on a test method, which will verify that all mock objects used in that method were called at least once. Finally, you can use the @InjectMocks annotation on a test method to inject all mock objects used in that method into a real object, and then call verify() on that real object.

14. How do you test private methods using Mockito?

You can test private methods using Mockito’s @InjectMocks annotation. This annotation will inject mock dependencies into the class that is being tested. Once the dependencies are injected, you can then use Mockito to verify that the private methods are being called as expected.

15. How do you check if an exception has been thrown by a particular method?

You can use the @Test(expected = Exception.class) annotation to check if an exception has been thrown by a particular method.

16. How does Mockito compare with other mocking frameworks like PowerMock or EasyMock?

Mockito is a popular mocking framework that is used with JUnit. Mockito is used to create mock objects that can be used in place of real objects in unit tests. Mockito is similar to other mocking frameworks like PowerMock or EasyMock, but it has a simpler API and it is easier to use.

17. Can you briefly describe the process used by Mockito to validate whether a method was invoked or not?

Mockito uses a process called verification to validate whether a method was invoked or not. This process involves recording all interactions with a mock object, and then later comparing those interactions against a set of expectations. If the recorded interactions match the expectations, then the verification process is successful.

18. How do you ensure that certain methods get executed at least once on a mock object?

You can use the verify() method on a mock object to ensure that a certain method gets executed at least once.

19. How do you verify a method was called one or more times on a mock object?

You can use the Mockito verify() method to verify that a method was called one or more times on a mock object.

20. How do you specify the order of execution of multiple methods on a single mock object?

You can use the @InjectMocks annotation to specify the order of execution of multiple methods on a single mock object.

Previous

20 Data Type Interview Questions and Answers

Back to Interview
Next

20 Liferay DXP Interview Questions and Answers