Re: Verifying ordered invocation of methods inside same mock

805 views
Skip to first unread message

Hari shankar

unread,
Mar 18, 2011, 3:20:33 PM3/18/11
to mockito
Am using v1.8.5 and this is the exception ST that am getting

org.mockito.exceptions.verification.VerificationInOrderFailure:
Verification in order failure
Wanted but not invoked:
junk.m2();
-> at test.mock.MockitoExpr.test2(MockitoExpr.java:67)
Wanted anywhere AFTER following interaction:
junk.m1();
-> at test.mock.MockitoExpr.test2(MockitoExpr.java:61)

    at test.mock.MockitoExpr.test2(MockitoExpr.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
   ...

On Sat, Mar 19, 2011 at 12:44 AM, har_shan <hars...@gmail.com> wrote:
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,

har_shan

unread,
Mar 18, 2011, 3:14:11 PM3/18/11
to mockito

Gergely Tóth

unread,
Mar 18, 2011, 4:23:58 PM3/18/11
to Hari shankar, moc...@googlegroups.com
Hi!

When you call m1 on the mock then since it's a mock it does not call m2(), you should create a real object and spy on it.

From docs:

13. Spying on real objects

You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed).

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");



2011/3/18 Hari shankar <hars...@gmail.com>
--
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.



--
Üdvözlettel
Tóth Gergely
Reply all
Reply to author
Forward
0 new messages