Top Mockito Interview Questions (2024) | TechGeekNext

Top Mockito Interview Questions (2024)

  1. What is mockito?
  2. What is stubbing in Mockito?
  3. Can we use Mockito to mock private methods?
  4. What is Mockito inline?
  5. What is Spy in unit testing?
  6. How to mock void method in mockito?
  7. Provide an example of using doNothing() for void method?
  8. Provide an example of using doAnswer() for void method?
  9. Provide an example of using doThrow() for void method?
  10. How to call another method in mock object
  11. What is the difference between thenReturn or doReturn in Mockito?

Q: What is mockito?
Ans:

Mockito is a JAVA-based mocking framework that is used for efficient unit testing of JAVA applications. Mockito is used to mock interfaces in order to add dummy functionality to a mock interface for use in unit testing.

Q: What is stubbing in Mockito?
Ans:

A stub is a fake class with predefined return values. It's injected into the test class to provide us complete control for testing the input.

Q: Can we use Mockito to mock private methods?
Ans:

Mockito does not support mocking private or static methods. To test private methods, we must modify the code to change the access to protected (or package), and you must avoid static/final methods. However, certain frameworks, like as PowerMock, offer mocking for private and static methods.

Take a look at our suggested post :

Q: What is Mockito inline?
Ans:

According to Mockito's Javadocs, "from version 2.7.6, it offers the'mockito-inline' artifact, which enables inline mock creation without specifying the MockMaker extension file" . Mockito includes advanced mocking features such as mocking final classes as an option. Use a separate mechanism ("mock maker") that is considered experimental and hence turned off by default to make this work.

Q: What is Spy in unit testing?
Ans:

While spying, we take an existing object and "replace" only a part of its methods. When we have a large class and only want to simulate a few methods, this is beneficial (partial mocking).

Q: How to mock void method in mockito?
Ans:

To call an actual method or a mock void method in Mockito, we can utilise a variety of methods. We can select one of the solutions based on our needs.

  1. doNothing(): Ignoring the call to the void method; this is the default action.
  2. When the void method is called, doAnswer() performs certain run-time or complicated procedures.
  3. When a mocked void method is called, doThrow() throws an exception.
  4. doCallRealMethod(): Do not mock and call the real method at the same time.

Q: Provide an example of using doNothing() for void method?
Ans:

@Test
public void testfindByName() {
   doNothing().when(mockedUserRepository).findByName(anyString());

   userService.findByName("techgeeknext");

   verify(mockedUserRepository, times(1)).findByName("techgeeknext");
}

Q: Provide an example of using doAnswer() for void method?
Ans:

@Test
public void testUpdateUser() {
   doAnswer(invocation -> {
      int userId = invocation.getArgument(0);
      String userName = invocation.getArgument(1);

      assertEquals(1, userId);
      assertEquals("techgeeknext", userName);

      return null;
}).when(mockedUserRepository).UpdateUser(anyInt(),anyString());

   userService.UpdateUser(1,"techgeeknext");
   verify(mockedUserRepository, times(1)).UpdateUser(1,"techgeeknext");
}

Q: Provide an example of using doThrow() for void method?
Ans:

@Test(expected = SQLException .class)
public void testDBConnection(){
 Connection connection = mock(Connection.class);
 doThrow(new SQLException()).when(connection).close();
 DatabaseUtils.closeQuietly(connection);
 // for no failure
 verify(connection).close(); // make sure to close
}

Q: How to call another method in mock object?
Ans:

@Test
public void testAddUserFlow() {
    User user = Mockito.mock(User.class);

    Mockito.doCallRealMethod().when(user).add();
    Mockito.doCallRealMethod().when(user).delete();

        user.delete().onConfirm();

    Mockito.verify(user).delete();
}

Q: What is the difference between thenReturn or doReturn in Mockito?
Ans:

When we know the return value at the time we mock a method call, we can use thenReturn or doReturn. When we call the mocked method, this defined value is returned. thenReturn(T value) specifies the return value that will be returned when the method is invoked.

  1. Whenever we need a fixed return value from a method call, we should use thenReturn().
  2. Whenever we need to do an operation or compute a value at run time, we should utilise thenAnswer().
@Test
public void testGetUserByName() throws Exception {
    // mock user repo
    int resturnUserId = 5;

    // write according to requirement and options
    when(userRepo.getUserByName("techgeeknext")).thenReturn(resturnUserId);
    doReturn(resturnUserId).when(userRepo).getUserByName("techgeeknext");
}

Recommendation for Top Popular Post :