« Return to Thread: Mocking Database Access - unexpected invocation

Re: Mocking Database Access - unexpected invocation

by Julian Hall-2 :: Rate this Message:

Reply to Author | View in Thread

Winfried wrote:

> I try to mock the access to my database to check some programmatical logic
>
> the test looks like
>
> try {
>  context.checking(new Expectations() {{
>   oneOf(mockDBGroup).getGroupByGroupId(pGroup.getGroupId(),
> newTransactionManagerImpl().getTransaction()); will(returnValue(pGroup));
>   oneOf(mockDBUser).getUser(new DatabaseQueryBean(), new
> TransactionManagerImpl().getTransaction());
> will(returnValue(pUserLoggedIn));
>  }});
> } catch (SQLException e) {
>   e.printStackTrace();
>   fail();
> }
>
> service.addGroupMember(pUserLoggedIn, pUser.getUserId(),
> pGroup.getGroupId(), pPassword, pRole);
> context.assertIsSatisfied();
>
> the omplementation of the group member method contains the following code
>
> DatabaseQueryBean queryBean = new DatabaseQueryBean();
> queryBean.addFilter( new FilterItem( "user_id", pUserId ) );
> user = dbUser.getUser( queryBean, tx );
>
> The problem now is that i get an unexpected invocation when i run this test.
> I asume that's becaus jmock expects a call with the the object instantiated
> in the test as parameter but gets a new object. Is this correct an is there
> any way avoiding to use the same object?
>
> Thanks for any advice
> wkurtz
>  
Yes, the default behaviour is to require an object that is equal to the
one you specify in the expectations.  You should try:

oneOf(mockDBUser).getUser(with(any(DatabaseQueryBean.class)), with(any([whatever class your transactions are].class)));

Or use one of the other matchers rather than 'any', if they are more
appropriate.  See the javadoc for org.jmock.Expectations for a list of
the common ones.

Hope this helps,

Jules

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: Mocking Database Access - unexpected invocation