rahmandon / mockito

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

Better failure messages: show the parameters of other interactions with a mock #380

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
In Mockito 1.9.5-rc1, when some expected method call is not done on the mock, 
the failure message looks something like this:

Wanted but not invoked:
client.startJob(
    "myJob",
    {constant=foo, variable=2}
);
-> at com.example.SomeTest.test(SomeTest.java:138)

However, there were other interactions with this mock:
-> at com.example.SomeClass.release(SomeClass.java:238)

The problem with the above failure message is that it does not show the runtime 
parameters of those other interactions. It would help in understanding what the 
system is doing, if it would show the parameters of each method call.

Here is what the improved failure message could look like:

Wanted but not invoked:
client.startJob(
    "myJob",
    {constant=foo, variable=2}
);
-> at com.example.SomeTest.test(SomeTest.java:138)

However, there were other interactions with this mock:
client.startJob(
    "myJob",
    {constant=foo, variable=1}
);
-> at com.example.SomeClass.release(SomeClass.java:238)

Original issue reported on code.google.com by esko.luo...@gmail.com on 27 Sep 2012 at 12:08

GoogleCodeExporter commented 8 years ago
ok, this should be failry easy to create

Original comment by brice.du...@gmail.com on 27 Sep 2012 at 12:20

GoogleCodeExporter commented 8 years ago
Fixed in revision 49f00f9d3cb839194b5052e27e5ac6230f9f2424

Original comment by brice.du...@gmail.com on 27 Sep 2012 at 2:23

GoogleCodeExporter commented 8 years ago
Thanks!

Original comment by esko.luo...@gmail.com on 27 Sep 2012 at 2:53

GoogleCodeExporter commented 8 years ago
It wasn't really a hard fix :)

Original comment by brice.du...@gmail.com on 27 Sep 2012 at 2:55

GoogleCodeExporter commented 8 years ago
Hey Esko,

In your use case, Mockito throws a different exception (ArgumentsAreDifferent) 
and the message does contain the actual parameters. Can you confirm that?

Showing all args for all invocations might be a good idea on not ;). We should 
make sure it does not clutter the view.

Original comment by szcze...@gmail.com on 12 Oct 2012 at 9:20

GoogleCodeExporter commented 8 years ago
Here is an example:

import org.junit.Test;

import static org.mockito.Mockito.*;

public class Example {

    @Test
    public void test1() {
        AnInterface m = mock(AnInterface.class);

        for (int i = 1; i <= 2; i++) {
            m.foo(i);
        }

        verify(m).foo(1);
        verify(m).foo(2);
        verify(m).foo(3); // XXX: doesn't mention the parameters of foo(1) and foo(2)
        verify(m).foo(4);
    }

    @Test
    public void test2() {
        AnInterface m = mock(AnInterface.class);

        for (int i = 1; i <= 4; i++) {
            m.foo(i);
        }

        verify(m).foo(1);
        verify(m).foo(2);
        verify(m).foo(5); // XXX: doesn't mention foo(4) at all
    }

    public interface AnInterface {
        void foo(int i);
    }
}

Original comment by esko.luo...@gmail.com on 13 Oct 2012 at 12:14

GoogleCodeExporter commented 8 years ago
I don't think this issue is fixed :). I may be wrong though - I haven't tested 
it - I've just had a brief look at the code in master. Brice, if the cases 
submitted by Esko work feel free to close the ticket!

Original comment by szcze...@gmail.com on 10 Nov 2012 at 7:18

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 8 Jan 2013 at 4:57