简体   繁体   中英

Nested mocking in Mockito

I have this Mockito code:

interface Dao {
    public void doSomething();
}

class LegacyClass {
    Dao dao;

    public String legacyMethod() {
        dao.doSomething();
        return "Test";
    }
}

public class MockitoTest {
    public static void main(String[] args) {
        Dao dao = mock(Dao.class);
        LegacyClass legacyInst = new LegacyClass();
        legacyInst.dao = dao;
        LegacyClass legacy = spy(legacyInst);

        when(legacy.legacyMethod()).thenReturn("Replacement");
    }
}

The last when() throws the following exception:

Exception in thread "main" org.mockito.exceptions.base.MockitoException: 
'doSomething' is a *void method* and it *cannot* be stubbed with a *return value*!
Voids are usually stubbed with Throwables:
    doThrow(exception).when(mock).someVoidMethod();
If the method you are trying to stub is *overloaded* then make sure you are calling the right overloaded version.
    at mypkg.MockitoTest.main(MockitoTest.java:28)

However, I am NOT mocking return value for Dao.doSomething , but for LegacyClass.legacyMethod() .

Is this the expected behavior? Are there any Mockito docs stating you cannot nest mocks like this?

How can I walk this around?

Spies don't work this way. In your sample code, the real method legacy.legacyMethod() is actually called because it's a spy not a mock (which then calls dao.doSomething() ), that's why you are getting this error.

If you want to make a partial mock, you have to write this as :

doReturn("Replacement").when(legacy).legacyMethod();

That way Mockito will know that you want to make a partial mock, so it won't call the real method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM