amorfis
amorfis

Reputation: 15770

Something like Mockito's Answer for Spock?

In Mockito there is nice method for programatic answers from interactions with mocks. E.g. we can program mock to return parameter which was passed to it's method call:

when(mockDao.persist(any(Entity.class)).thenAnswer(new Answer<Entity>() {

    public Entity answer(InvocationOnMock invocationOnMock) throws Throwable {
        Entity entity = (Entity) invocationOnMock.getArguments()[0];

        return entity;
    }
});

Is there a way to do the same in Spock?

Upvotes: 1

Views: 1002

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123920

mockDao.persist(_) >> { it[0] }

Or, with destructuring:

mockDao.persist(_) >> { Entity entity -> entity }

Upvotes: 5

Related Questions