ssacher-tgm / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Two InOrder's on same mock are not independent #163

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I expect two series of call orders on same mock, but these orders are not
related to each other, but Mockito related two InOrder instances to each
other by default.

In other words, the code below fails:

    @Test
    public void inOrderTest(){
        List list=mock(List.class);

        list.add("a");
        list.add("x");
        list.add("b");
        list.add("y");

        InOrder inOrder = inOrder(list);
        InOrder inAnotherOrder = inOrder(list);
        assertNotSame(inOrder, inAnotherOrder);

        inOrder.verify(list).add("a");
        inOrder.verify(list).add("b");

        inAnotherOrder.verify(list).add("x");
        inAnotherOrder.verify(list).add("y");
    }

I know it seems like a code smell to instantiate two different orders in
same test case, but guess this is a bug. 

Original issue reported on code.google.com by bahri.ge...@gmail.com on 20 Jan 2010 at 3:16

GoogleCodeExporter commented 8 years ago
Hmmm, interesting.

Is this a real case from the code or you are testing mockito in anger?

Quickly thinking about implementation it seems quite difficult so I'm not sure 
I will
fix it soon.

Original comment by szcze...@gmail.com on 20 Jan 2010 at 6:42

GoogleCodeExporter commented 8 years ago
Yes, I did encounter this issue on a real test case where I was expecting two
different kind of events on same trigger. I have no hatred against Mockito, 
indeed it
is now my favourite mocking framework :)

The test case is of course only for reference.

I know, refactoring the test into the one below works around the problem and it 
is
arguably a better test case:

    @Test
    public void abInorder(){
        List list=mock(List.class);

        fill(list);

        InOrder inOrder = inOrder(list);

        inOrder.verify(list).add("a");
        inOrder.verify(list).add("b");   
    }

    @Test
    public void xyInorder(){
        List list=mock(List.class);

        fill(list);

        InOrder inOrder = inOrder(list);

        inOrder.verify(list).add("x");
        inOrder.verify(list).add("y");
    }

    private void fill(List list) {
        list.add("a");
        list.add("x");
        list.add("b");
        list.add("y");
    }

Original comment by bahri.ge...@gmail.com on 20 Jan 2010 at 7:04

GoogleCodeExporter commented 8 years ago
Oh I see ;)

Are you willing to take a stab at implementation?

Original comment by szcze...@gmail.com on 20 Jan 2010 at 7:10

GoogleCodeExporter commented 8 years ago
I think I've encountered the same problem. I wanted to test several ordered 
events in
the same test, the order in the individual series of events was important, but 
not
the order between the series. It worked on my local machine, but not on our CI 
server.

What I mean to say is that I agree there are use cases for this scenario, other 
than
"testing Mockito in anger" :)

Original comment by daniel.b...@gmail.com on 10 Feb 2010 at 1:03

GoogleCodeExporter commented 8 years ago
This issue is waiting for the patch from users

Original comment by szcze...@gmail.com on 14 Feb 2010 at 12:34

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 14 Feb 2010 at 9:54

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 11 Mar 2010 at 10:24

GoogleCodeExporter commented 8 years ago
This issue was closed by revision r1921.

Original comment by szcze...@gmail.com on 14 Mar 2010 at 9:22

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 17 Mar 2010 at 8:32