freepascal / mockito

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

any() matcher behaviour differs from anyObject() for nulls #73

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In Mockito 1.7, the new any( Class ) matcher says that it is an alias for
anyObject(), but it is actually an alias for isA(), which is different in
the way it handles null values. anyObject() matches nulls; isA() does not.

I think it's useful to have both, but I would prefer any() to follow
anyObject(), considering the naming convention. And isA() already provides
(IMO) a clearer way to test for non-null values. At a minimum, the Javadoc
description of anyObject() should be corrected if the current behaviour is
to stay, to avoid confusion.

The attached test case demonstrates the problem.

Original issue reported on code.google.com by mhack...@kanayo.com on 17 Apr 2009 at 2:47

Attachments:

GoogleCodeExporter commented 9 years ago
You're right - we'll fix it

Original comment by szcze...@gmail.com on 17 Apr 2009 at 2:52

GoogleCodeExporter commented 9 years ago
Fixed in trunk.
any(T.class) is now an alias to anyObject()

Original comment by szcze...@gmail.com on 19 Apr 2009 at 5:31

GoogleCodeExporter commented 9 years ago
Is there a work around for this? How do we match on a null while not returning 
an Object?

Original comment by antony.s...@gmail.com on 20 May 2009 at 11:52

GoogleCodeExporter commented 9 years ago
You match for null just using null keyword. You match not-nulls using notNull()
matcher (or something similar, don't remember now).

Original comment by szcze...@gmail.com on 20 May 2009 at 12:17

GoogleCodeExporter commented 9 years ago
Thanks For the response. 
I can't do that in my situation as it's a two parameter function call, the 
first I want to use anyInt and the 
second I want to use null. But you can't mix matched with value. Null being the 
value.   

Original comment by antony.s...@gmail.com on 20 May 2009 at 12:23

GoogleCodeExporter commented 9 years ago
To demonstrate, I think this works:
        when( mockProfileManager.getProfileByPersonIdAndNetwork( anyInt(), argThat( new 
ArgumentMatcher<Network>(){

            @Override
            public boolean matches(Object argument) {
                return true;
            }

        } ) ) ).thenReturn( profile );

Original comment by antony.s...@gmail.com on 20 May 2009 at 12:27

GoogleCodeExporter commented 9 years ago
Try isNull() matcher or simply eq(null) :)

Original comment by szcze...@gmail.com on 20 May 2009 at 1:30

GoogleCodeExporter commented 9 years ago
Yeah I thought so - I tried that as well before I posted here.
isNull gives The method getProfileByPersonIdAndNetwork(int, Network) in the 
type ProfileManagerImpl is not 
applicable for the arguments (int, Object)  NavigationPanelTest.java    invite-
commons/src/test/java/com/componence/invite/panelTests  line 112    Java Problem
eq(null) also gives the same error.

Original comment by antony.s...@gmail.com on 20 May 2009 at 8:05

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 9 Jun 2009 at 3:15