Reputation: 584
Is it possible to call some method from another mock method instead of returning a value using Mockito or PowerMock ?
Here is example to make in clear:
I have a query in production class:
session.createQuery("update clause")
.setParameter("")
.executeUpdate();
session.flush();
And in my test class I mock it this way:
Query q = mock(Query.class, "q");
when(session.createQuery("update lalala")).thenReturn(q);
when(q.executeUpdate()).thenReturn(something);
Now instead of doing thenReturn(something)
I need to call a void method that's situated in my test class too that mocks database behaviour.
i.e.
public void doSomething()
{
// do smth
}
So that in my test when q.executeUpdate is called then doSomething() is then called too.
I googled for any possible ideas but just can't seem to figure it out.
Upvotes: 2
Views: 251
Reputation: 5930
You can use the thenAnswer
function. See the documentation
when(q.executeUpdate()).thenAnswer( new Answer<Foo>() {
@Override
public Foo answer(InvocationOnMock invocation) throws Throwable {
callYourOtherMethodHere();
return something;
}
} );
Upvotes: 4
Reputation: 3141
You can do something like this with EasyMock (maybe with other mocking tools too).
Query q = createMock(Query.class)
expect(q.createQuery("update lalala")).andDelegateTo(anObjectThatCalls_doSomething);
...
Upvotes: 0