How to go about deep mock or stub with Spock?
Asked Answered
S

1

7

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);
Septet answered 21/10, 2016 at 7:46 Comment(0)
D
6

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.

Demogorgon answered 2/11, 2016 at 10:55 Comment(1)
I think you need to change changes() >> aChangeApi to current() >> aChangeApi, to properly match the example provided in the question.Presbyterial

© 2022 - 2024 — McMap. All rights reserved.