-1

Trying mock void method using JUnit4 then getting below exception

Below is the class definition

@Mock
private TestService service;

@Mock
private DatabaseService mockDatabase;

@Before
public void setUpMock() throws Exception {

    LOGGER.info("########### Moke setUp started ###########");

    conn = Mockito.mock(Connection.class);
    MockitoAnnotations.initMocks(this);

    LOGGER.info("########### Moke setUp completed ###########");
}


@Test
public void testSuccess() {
    try {
        
        when(mockDatabase.getDBConnection()).thenReturn(conn);
        Mockito.doNothing().when(service).deleteKeyInfo(mockDatabase.getDBConnection(), userBn, 1, "1");
    } catch (Exception e) {
        fail("### testDeviceIdNull ### Failed with following error: " + getStackTrace(e));
    }
}

But getting below exception

java.lang.AssertionError: ### Failed with following error: org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:


E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3. you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed

Below is the void method

public void deleteKeyInfo(Connection conn, UserBn userBn, Integer smId, String dId) throws Exception {
    // Deleting key
        
}
Lesiak
  • 22,088
  • 2
  • 41
  • 65
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • I haven't used Mockito in a while, but I believe the current preferred style is `when(service.deleteKeyInfo()).doNothing()`. (Also, I believe the `service.delete` should all be inside the `when`.) – chrylis -cautiouslyoptimistic- Jul 09 '20 at 18:05
  • For the root cause of the error, we need more context: what is `service` (mock or spy) `doNothing()` is default action for void methods for mocks. dropping the problematic line will help if `service` is a mock. what is `mockDatabase`? How you specified behaviour of `mockDatabase.getDBConnection()`? – Lesiak Jul 09 '20 at 18:37
  • @Lesiak Updated the question with more info – Shiladittya Chakraborty Jul 10 '20 at 04:42

1 Answers1

3

You're nesting mocking inside of mocking. You're calling getDBConnection(), which does some mocking, before you've finished the mocking for service.

Mockito doesn't like it when you do this.

Replace:

try {
        
        when(mockDatabase.getDBConnection()).thenReturn(conn);
        Mockito.doNothing().when(service).deleteKeyInfo(mockDatabase.getDBConnection(), userBn, 1, "1");
    }

with:

try {
        
        when(mockDatabase.getDBConnection()).thenReturn(conn);
        some_variable_name =  mockDatabase.getDBConnection();         
        Mockito.doNothing().when(service).deleteKeyInfo(some_variable_name, userBn, 1, "1");
    }

Please check this : Unfinished Stubbing Detected in Mockito for more information

Sanket Singh
  • 1,246
  • 1
  • 9
  • 26
  • 2 minor comments: 1. You don't need a temporary variable, just use conn. 2 As an alternative, Mockito.doNothing is the default for void methods, the entire call can be dropped. – Lesiak Jul 10 '20 at 07:00