|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Mocking Database Access - unexpected invocationI 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 |
|
|
Re: Mocking Database Access - unexpected invocationWinfried 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 > 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 |
|
|
Re: Mocking Database Access - unexpected invocation2009/7/1 Winfried <w.kurtz@...>:
> 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? Yes, that's why you're getting the failure. > Is there any way avoiding to use the same object? You need to either pass the objects that the DBGroup needs into the service, or loosen the constraints on the parameters to the mocked calls. For example, the service can be given the TransactionManager when it was created (e.g. pass the TransactionManager to its constructor). Then your test can mock out the TransactionManager and the Transaction it creates, and that transaction can be used in expectations on the DBGroup and DBUser. --Nat -- http://www.natpryce.com --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Mocking Database Access - unexpected invocationYes, 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))); That worked, Thanks a lot Jules |
| Free embeddable forum powered by Nabble | Forum Help |