Show List

Mockito Interview Questions


  • What is Mockito?
    Answer: Mockito is a popular open-source mocking framework used for testing Java applications.

  • Why is Mockito used for testing?
    Answer: Mockito is used to create mock objects for testing. These mock objects can be used to isolate the unit under test and verify its behavior.

  • How does Mockito work?
    Answer: Mockito creates a mock object that can be used to replace a real object during testing. The mock object can be used to stub methods, simulate exceptions, and verify method calls.

  • What is a mock object?
    Answer: A mock object is a replacement for a real object that is used during testing. A mock object is created using a mocking framework like Mockito and can be used to isolate the unit under test and verify its behavior.

  • What is a stub in Mockito?
    Answer: A stub is a method that returns a specific value when called. Mockito allows you to create stubs for methods using the when-then pattern.

  • What is a spy in Mockito?
    Answer: A spy is a real object that is wrapped in a Mockito object. The spy allows you to monitor the behavior of the real object and stub its methods.

  • What is the difference between a mock and a spy in Mockito?
    Answer: A mock is a completely fake object that is used to isolate the unit under test. A spy is a real object that is wrapped in a Mockito object and allows you to monitor its behavior.

  • What is the @Mock annotation in Mockito?
    Answer: The @Mock annotation is used to create a mock object for a class. The mock object can be used to stub methods and verify behavior.

  • What is the @InjectMocks annotation in Mockito?
    Answer: The @InjectMocks annotation is used to inject the mock objects created with the @Mock annotation into the class being tested.

  • What is the @Spy annotation in Mockito?
    Answer: The @Spy annotation is used to create a spy object for a class. The spy object can be used to monitor the behavior of the real object.

  • What is the difference between a @Mock and a @Spy annotation in Mockito?
    Answer: The @Mock annotation creates a completely fake object, while the @Spy annotation wraps a real object in a Mockito object.

  • What is the Mockito.verify() method used for?
    Answer: The Mockito.verify() method is used to verify that a method was called with the expected arguments.

  • What is the Mockito.any() method used for?
    Answer: The Mockito.any() method is used to match any argument passed to a method during testing.

  • What is the Mockito.doThrow() method used for?
    Answer: The Mockito.doThrow() method is used to simulate an exception being thrown by a method during testing.

  • What is the Mockito.doAnswer() method used for?
    Answer: The Mockito.doAnswer() method is used to customize the behavior of a method during testing.

  • What is the difference between the doThrow() and thenThrow() methods in Mockito?
    Answer: The doThrow() method is used to throw an exception before the method is called, while the thenThrow() method is used to throw an exception after the method is called.

  • What is the difference between the doReturn() and thenReturn() methods in Mockito?
    Answer: The doReturn() method is used to return a value before the method is called, while the thenReturn() method is used to return a value after the method is called.

  • What is the difference between a mock and a spy in Mockito? Answer: A mock is a completely fake object that is used to isolate the unit under test. A spy is a real object that is wrapped in a Mockito object and allows you to monitor its behavior.

  • What is the @Mock annotation in Mockito?
    Answer: The @Mock annotation is used to create a mock object for a class. The mock object can be used to stub methods and verify behavior.

  • What is the @InjectMocks annotation in Mockito?
    Answer: The @InjectMocks annotation is used to inject the mock objects created with the @Mock annotation into the class being tested.

  • What is the @Spy annotation in Mockito?
    Answer: The @Spy annotation is used to create a spy object for a class. The spy object can be used to monitor the behavior of the real object.

  • What is the difference between a @Mock and a @Spy annotation in Mockito?
    Answer: The @Mock annotation creates a completely fake object, while the @Spy annotation wraps a real object in a Mockito object.

  • What is the Mockito.verify() method used for?
    Answer: The Mockito.verify() method is used to verify that a method was called with the expected arguments.

  • What is the Mockito.any() method used for?
    Answer: The Mockito.any() method is used to match any argument passed to a method during testing.

  • What is the Mockito.doThrow() method used for?
    Answer: The Mockito.doThrow() method is used to simulate an exception being thrown by a method during testing.

  • What is the Mockito.doAnswer() method used for?
    Answer: The Mockito.doAnswer() method is used to customize the behavior of a method during testing.

  • What is the difference between the doThrow() and thenThrow() methods in Mockito?
    Answer: The doThrow() method is used to throw an exception before the method is called, while the thenThrow() method is used to throw an exception after the method is called.

  • What is the difference between the doReturn() and thenReturn() methods in Mockito?
    Answer: The doReturn() method is used to return a value before the method is called, while the thenReturn() method is used to return a value after the method is called.

  • What is the difference between stubbing and mocking in Mockito?
    Answer: Stubbing is used to provide specific responses for method calls during testing, while mocking is used to create fake objects that can be used to isolate the unit under test.

  • What is the difference between a mock and a spy in Mockito?
    Answer: A mock is a completely fake object that is used to isolate the unit under test. A spy is a real object that is wrapped in a Mockito object and allows you to monitor its behavior.

  • What is the Mockito.when() method used for?
    Answer: The Mockito.when() method is used to stub a method call with a specific response during testing.

  • What is the difference between @Mock and @MockBean in Spring Boot testing?
    Answer: @Mock is used in unit tests to create a mock object for a class, while @MockBean is used in integration tests to create a mock object for a Spring Bean.

  • What is the difference between @InjectMocks and @Autowired in Spring Boot testing?
    Answer: @InjectMocks is used to inject the mock objects created with @Mock into the class being tested, while @Autowired is used to inject the real dependencies into the class being tested.
  • What is the purpose of the Mockito framework?
    Answer: The purpose of the Mockito framework is to provide a way to create mock objects for unit testing.

  • What is a mock object?
    Answer: A mock object is a fake object that is used to isolate the unit under test from its dependencies.

  • What is the difference between a stub and a mock object?
    Answer: A stub is a fake object that returns pre-determined responses to method calls, while a mock object is a fake object that can be programmed to simulate the behavior of a real object.

  • What is the difference between a spy and a mock object?
    Answer: A spy is a real object that is wrapped in a Mockito object and allows you to monitor its behavior, while a mock object is a completely fake object that is used to isolate the unit under test.

  • How do you create a mock object in Mockito?
    Answer: You can create a mock object in Mockito by using the @Mock annotation or the Mockito.mock() method.

  • What is the difference between a mock and a dummy object?
    Answer: A dummy object is a placeholder object that is used to satisfy a method parameter, while a mock object is a fake object that is used to isolate the unit under test.

  • What is the difference between a fake object and a mock object?
    Answer: A fake object is a simplified implementation of a real object that is used in testing, while a mock object is a fake object that can be programmed to simulate the behavior of a real object.

  • What is the difference between a unit test and an integration test?
    Answer: A unit test is used to test a single component of an application in isolation, while an integration test is used to test the interactions between multiple components of an application.

  • How do you use the Mockito framework to verify that a method was called?
    Answer: You can use the Mockito.verify() method to verify that a method was called with the expected arguments.

  • How do you use the Mockito framework to stub a method call?
    Answer: You can use the Mockito.when() method to stub a method call with a specific response.

  • What is the difference between the thenReturn() and the thenThrow() methods in Mockito?
    Answer: The thenReturn() method is used to specify a return value for a stubbed method call, while the thenThrow() method is used to specify an exception to be thrown.

  • What is the difference between the @Mock and @Spy annotations in Mockito?
    Answer: The @Mock annotation creates a completely fake object, while the @Spy annotation wraps a real object in a Mockito object.

  • How do you use the Mockito framework to test exceptions and error conditions?
    Answer: You can use the Mockito.doThrow() method to simulate an exception being thrown by a method during testing.

  • How do you use the Mockito framework to test private methods?
    Answer: You can use the Mockito framework in combination with PowerMock to test private methods.

  • What is the difference between the @Mock and @MockBean annotations in Spring Boot testing?
    Answer: @Mock is used to create a mock object for a class in a unit test, while @MockBean is used to create a mock object for a Spring Bean in an integration test.

  • What is the difference between the @InjectMocks and @Autowired annotations in Spring Boot testing?
    Answer: @InjectMocks is used to inject the mock objects created with @Mock into the class being tested, while @Autowired is used to inject the real dependencies into the class being tested.

    Leave a Comment


  • captcha text