Hi, am new to Mockito and starting to experiment it but got struck
with this use case.
I just wanted to verify that methods in a class (that's mocked) are
being invoked in order that it is supposed to.
Googled a bit but couldn't understand/get a clean solution. Excuse if
this is trivial.
@Test
public void test2() throws Exception {
// mock creation
Junk mockedJunk = mock(Junk.class);
// using mock object
mockedJunk.m1();
InOrder orderedVerif = inOrder(mockedJunk);
// verification - we just called m1() but it should have called m2()
and then m3() in order
orderedVerif.verify(mockedJunk).m1();
orderedVerif.verify(mockedJunk).m2(); // fails!
orderedVerif.verify(mockedJunk).m3(); // fails!
}
static class Junk {
void m1() {
m2();
}
void m2() {
m3();
}
void m3() {
}
}
What am i missing?!
i came across this http://amolbrid.wordpress.com/2010/12/07/mockito-verify-order-of-invocations/
Working code demonstrated there:
private Socket socketMock = mock(Socket.class);
public class RequestProcessor {
private Socket socket;
public RequestProcessor(Socket socket) {
this.socket = socket;
}
public void process() throws IOException {
socket.getOutputStream();
socket.getInputStream();
}
}
@Test
public void socketClosedInIdealCase() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream("a=b
+c".getBytes());
when(socketMock.getInputStream()).thenReturn(bais);
when(socketMock.getOutputStream()).thenReturn(baos);
RequestProcessor request = new RequestProcessor(socketMock);
request.process();
// verify method invocations
InOrder order = inOrder(socketMock);
order.verify(socketMock).getOutputStream();
order.verify(socketMock).getInputStream();
}
So should i add something like
when(..).thenReturn(..)
for all the methods that i want to verify to be invoked in order? if
so, could you pls explain why and a bit background..
Thanks much,
Real spies should be used carefully and occasionally, for example when dealing with legacy code.
Spying on real objects can be associated with "partial mocking" concept. Before the release 1.8, Mockito spies were not real partial mocks. The reason was we thought partial mock is a code smell. At some point we found legitimate use cases for partial mocks (3rd party interfaces, interim refactoring of legacy code, the full article is here)
List list = new LinkedList(); List spy = spy(list); //optionally, you can stub out some methods: when(spy.size()).thenReturn(100); //using the spy calls real methods spy.add("one"); spy.add("two"); //prints "one" - the first element of a list System.out.println(spy.get(0)); //size() method was stubbed - 100 is printed System.out.println(spy.size()); //optionally, you can verify verify(spy).add("one"); verify(spy).add("two");
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.