NullPointerException using two given with argThat on same method

1,708 views
Skip to first unread message

Cristiano Gavião

unread,
Nov 18, 2009, 3:57:40 PM11/18/09
to moc...@googlegroups.com
Hi people,

I have a classe that use a Dao's method that receive a List as parameter. I need two diffent responses depend on the size of list arg passed.

I've created a Custom ArgumentMatcher to be possible to known how may object is on list passed as parameter so it could return the right list.

I mock the dao method in this way:

        given(
                contractDAO
                        .findContracts(argThat(new IsListOfTwoElements())))
                .willReturn(getTwoConstracts());

        given(
                contratoArrecadacaoDAO
                        .findContracts(argThat(new IsListOfOneElement())))
                .willReturn(getOneContract());


And here are the custom arguments:

    @SuppressWarnings("unchecked")
    class IsListOfTwoElements extends
            ArgumentMatcher<List> {
        public boolean matches(Object list) {
            return ((List) list).size() == 2;
        }
    }

    @SuppressWarnings("unchecked")
    class IsListOfOneElement extends ArgumentMatcher<List> {
        public boolean matches(Object list) {
            return ((List) list).size() == 1;
        }
    }

When I use ONLY (I comment the first ) one given() its works nicely...  But when I try to run with both "given"  I'm getting a NullPointerException in the IsListOfOneElement argumentMatcher , because the list parameter passed is null (return ((List) list).size() )

It's a king strange, because I put a breaking point on the matches(Object list) IsListOfOneElement. When I run with only one "given" it stops on the break only when the real method is called. But when I run with both "given" it stops after the second "given"

Am I doing something wrong?


Thanks for any help...

Cristiano


szczepiq

unread,
Nov 18, 2009, 6:17:24 PM11/18/09
to moc...@googlegroups.com
> Am I doing something wrong?

Nope. It's a long story and I have to go to sleep so let me share with
you a short story :)

Consider it as a mocking syntax trade-off and for that case please use:

willReturn(xxx).given(contractDAO).find....

It should be also in the docs if you read through willReturn()...
doReturn()... javadocs or related content.

Cheers!
Szczepan Faber
> --
>
> 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.
>

Tiago Martins

unread,
Sep 5, 2014, 6:56:17 AM9/5/14
to moc...@googlegroups.com
I'm curious with the long story. Can you share it, pleaser?

Or a link pointing to the issue?


Cheers,
Tiago

Szczepan Faber

unread,
Sep 5, 2014, 8:41:58 AM9/5/14
to moc...@googlegroups.com
Take a look at javadocs for the doReturn method.

Cheers!
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mockito+u...@googlegroups.com.
> To post to this group, send email to moc...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mockito.
> For more options, visit https://groups.google.com/d/optout.



--
Szczepan Faber
Core dev@gradle; Founder@mockito

Szymon Fiedorek

unread,
Sep 7, 2017, 4:39:01 AM9/7/17
to mockito

Thank You very much Szczepan :) saved my day

Viktor Horvath

unread,
Mar 15, 2019, 12:49:43 PM3/15/19
to mockito
Absolutely brilliant Szczepan ! You saved my day too, thanks!
Reply all
Reply to author
Forward
0 new messages