verify() says mocked method not called?

7,328 views
Skip to first unread message

Russell Bateman

unread,
Aug 29, 2012, 8:30:15 PM8/29/12
to moc...@googlegroups.com
I've stepped through my test carefully and the methods I'm verifying are in fact called. So, I'm puzzled as to the JUnit failure message. (In general, I've had a lot of success verifying called, mocked methods.) What am I missing? What should I be thinking about as I track down this problem?

Thanks,

Russ


Wanted but not invoked:
accountDao.readAllByIdentityoid(
    000000000000000000000099
);
-> at com.
acme.user.manager.PartnerManagerTest.testWalkAccounts(PartnerManagerTest.java:269)
Actually, there were zero interactions with this mock.

    at com.acme.user.manager.PartnerManagerTest.testWalkAccounts(PartnerManagerTest.java:269)
    ...


My code:

       
...
    private final ObjectId CLAPFISHOID = ObjectId( "000000000000000000000099" );
    ...
        // now mock the DAO methods called by PartnerManager and AccountManager...
        List< Account > accounts = new ArrayList< Account >();
        accounts.add( SCHMUCK );
        accounts.add( ACCOUNT1 );
        accounts.add( ACCOUNT2 );
        accounts.add( ACCOUNT3 );
        accounts.add( ACCOUNT4 );

        when( accountDao.readAllByIdentityoid(  SCHMUCKOID ) ).thenReturn( accounts );
        when( partnerDao.readByOid( CLAPFISHOID ) ).thenReturn( CLAPFISH );
        when( partnerDao.readByOid( PARTNEROID1 ) ).thenReturn( PARTNER1 );
        when( partnerDao.readByOid( PARTNEROID2 ) ).thenReturn( PARTNER2 );
        when( partnerDao.readByOid( PARTNEROID3 ) ).thenReturn( PARTNER3 );
        when( partnerDao.readByOid( PARTNEROID4 ) ).thenReturn( PARTNER4 );

        WalkFederatedAccounts walker = new WalkFederatedAccounts( CLAPFISHOID, SCHMUCKOID, "Test123" );

  269:  verify( partnerDao ).readByOid( CLAPFISHOID );
    <------ offending verify() call

        walker.byPassword();

        // these calls into the DAO should have been made during this test...
        verify( accountDao ).readAllByIdentityoid( SCHMUCKOID );
        verify( partnerDao ).readByOid( CLAPFISHOID );
        verify( partnerDao ).readByOid( PARTNEROID1 );
        verify( partnerDao ).readByOid( PARTNEROID2 );
        verify( partnerDao ).readByOid( PARTNEROID3 );
        verify( partnerDao ).readByOid( PARTNEROID4 );

        verify( accountDao ).update( ACCOUNT1 );
        verify( accountDao ).update( ACCOUNT2 );
        verify( accountDao ).update( ACCOUNT3 );

        walker = new WalkFederatedAccounts( CLAPFISHOID, SCHMUCKOID, "schmuck-...@schmucks-r-us.com" );

        verify( partnerDao ).readByOid( CLAPFISHOID );

        walker.byEmail();

        // these calls into the DAO should have been made during this test...
        verify( accountDao ).readAllByIdentityoid( SCHMUCKOID );
        verify( partnerDao ).readByOid( CLAPFISHOID );
        verify( partnerDao ).readByOid( PARTNEROID1 );
        verify( partnerDao ).readByOid( PARTNEROID2 );
        verify( partnerDao ).readByOid( PARTNEROID3 );
        verify( partnerDao ).readByOid( PARTNEROID4 );

        verify( accountDao ).update( ACCOUNT1 );
        verify( accountDao ).update( ACCOUNT2 );
        verify( accountDao ).update( ACCOUNT3 );

David Wallace

unread,
Aug 29, 2012, 8:39:14 PM8/29/12
to moc...@googlegroups.com
Russell,
What does the equals() method of ObjectId do?  When mockito does a verify like the one that's giving you a problem, it uses the appropriate equals() method to compare the argument to the call in the verify with the argument that was passed during the test.  Could this be what's going wrong?

Regards,
David Wallace.

--
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.

Russell Bateman

unread,
Aug 29, 2012, 11:39:00 PM8/29/12
to moc...@googlegroups.com
Hmmm... yeah, I don't have control over that; it belongs to the Mongo Java driver code. It could be that, while the "guts" of the id are identical, the Java object reference isn't and that ObjectId.equals() is only comparing the reference. I'll look closer at that tomorrow when I get back in and if so, I'll have to think hard on a solution.

Thanks,

Russ

Brice Dutheil

unread,
Aug 30, 2012, 3:48:04 AM8/30/12
to moc...@googlegroups.com
You could use an argument captor, so you'll be able to make other assertions on the passed reference.

Aside of that, I think this test is really big, it might show problematic to refactor it for new feature or maintenance purposes. You should look for opportunities to split it up :)

Cheers,

-- Brice Dutheil

nishanth machado

unread,
Aug 30, 2012, 8:43:37 AM8/30/12
to moc...@googlegroups.com
Use verify() method after the test method to be tested executes.

if suppose partenrDao.readByOid() is inside method walker.byPassword() then call the execution of walker.byPassword() first and then call verfy() method to check whether it invoked the stub method during its execution.

walker.byPassword();

Russell Bateman

unread,
Aug 30, 2012, 12:02:03 PM8/30/12
to moc...@googlegroups.com
Uh, okay, these comparisons are working, but I wasn't injecting my DAOs for use by the walker (I was only injecting them for PartnerManager whose code I wasn't showing), so the record of what was called with what arguments was faulty.

Thanks,

Russ

Russell Bateman

unread,
Aug 30, 2012, 12:07:55 PM8/30/12
to moc...@googlegroups.com
Turns out I don't have to resort to an argument matcher since the equals() does work.

Yes, I was just trying to get it all working before breaking it out, which it is now and both tests (password- and e-mail synchronization) work perfectly.

Once again, Mockito, what can be said? It's the cat's meow.

Russ

Brice Dutheil

unread,
Sep 3, 2012, 6:03:54 AM9/3/12
to moc...@googlegroups.com
It's the cat's meow

wow :)

-- Brice
Reply all
Reply to author
Forward
0 new messages