Search code examples
mockitospock

How to go about deep mock or stub with Spock?


How to do the equivalent of Mockito's deep mock / stub (RETURNS_DEEP_STUBS ) in Spock? Something like:

Changes changes = Mock()
changes.id(_).current() >> aChangeApi

While in Mockito it'd be:

Changes changes = mock(Changes.class, RETURNS_DEEP_STUBS);
when(changes.id(any()).current()).thenReturn(aChangeApi);

Solution

  • I think you can do something like:

    Changes changes = Stub()
    changes.id(_) >> Stub(<ReturnedClass>) {
        current() >> aChangeApi
    }
    

    This just returns a stub which can then be further mocked. I'm not that familiar with Mockito but from a bit of google searching this seems to be the way that should get a similar result.