CowOfDoom
unread,Sep 24, 2008, 9:57:00 PM9/24/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mockito
I have quite a few places in my code where a line of code exists that
looks like this:
if( userService.getUserByUsername(input).getRole().isAdminUser()) {
...
}
If I want to test this bit of code with Mockito, I have to do
something like this:
UserService mockUserService = mock(UserService.class);
UserObject mockUser = mock(UserObject.class);
stub(mockUserService.getUserByUsername("fred")).toReturn(mockUser);
Role mockRole = mock(Role.class);
stub(mockUser.getRole()).toReturn(role);
stub(mockRole.isAdminUser()).toReturn(true);
I was wondering about ways to shorten that call to something like:
UserService mockUserService = mock(UserService.class);
stub( mockUserService.getUserByUsername("fred").getRole().isAdminUser() ).toReturn(true);
which would have the property of creating several mock objects, each
one with exactly one stubbed method returning the next one in the
sequence. I'm wondering whether such a thing would be useful to
others (it'd be very useful in my code base, but maybe that means it's
a lousy code base), and what such a syntax might have to look like if
it could be implemented at all. Thoughts?