[mockito] verify times

1,708 views
Skip to first unread message

rpitre

unread,
May 10, 2010, 4:25:44 PM5/10/10
to mockito
I'm new to Mockito and trying to understand why my unit test passes
after trying to force it to fail. I'm only doing this to learn more
about Mockito.

I have a POJO that i wish to test:

public class EmployeeCreatedEventHandler {

@Autowired
private TimberlineEmployeeService timberlineEmployeeService;

public void handle(EmployeeCreatedEvent event) throws
CreateTimberlineEmployeeException {
//
// Create Timberlne Employee record
//

timberlineEmployeeService.createTimberlineEmployee(event.getEmployee());
}
}


My test class:

@RunWith(MockitoJUnitRunner.class)
public class EmployeeCreatedEventHandlerTest {

@Mock
private TimberlineEmployeeService mockTimberlineEmployeeService;

@InjectMocks
private EmployeeCreatedEventHandler handler = new
EmployeeCreatedEventHandler();

@Test
public void testHandle() {
EmployeeCreatedEvent mockEvent = mock(EmployeeCreatedEvent.class);

handler.handle(mockEvent);

verify(mockTimberlineEmployeeService,
times(2)).createTimberlineEmployee(mockEvent.getEmployee());
}
}


I'm trying to force the test to fail by verifying that the
mockTimberlineEmployeeService will call the createTimberlineEmployee
method twice (times(2)) but it does not fail, it passes with green
bars.

However, i was able to make the test fail after changing the argument
to the createTimberlineEmployee method like so:

verify(mockTimberlineEmployeeService,
times(2)).createTimberlineEmployee(any(EmployeeType.class));

Can anybody tell me why?












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

Kristofer Karlsson

unread,
May 10, 2010, 4:59:35 PM5/10/10
to moc...@googlegroups.com
Here's my best guess:
Mockito gets confused by the call to mockEvent.getEmployee() inside the verification (since that's also a mock object), so Mockito is not sure what mock to verify interaction on.

Russell Pitre

unread,
May 10, 2010, 5:11:10 PM5/10/10
to moc...@googlegroups.com
I figured as much, i just wanted to know why it was getting confused.
If I "unmock" the EmployeeCreatedEvent class and instead instantiate
using the new operator, the test will fail as expected.

EmployeeCreatedEvent event = new EmployeeCreatedEvent();
handler.handle(event);
verify(mockTimberlineEmployeeService,
times(2)).createTimberlineEmployee(event.getEmployee());


So, as a general rule of thumb, is it safe to say we should not supply
mocks to methods in which are being verified?

szczepiq

unread,
May 11, 2010, 4:29:24 AM5/11/10
to moc...@googlegroups.com
Hey,

What version of Mockito do you use?

What you can do is this:

   Employee e = event.getEmployee();
   verify(mockTimberlineEmployeeService).createTimberlineEmployee(e);

However, since getEmployee() method is not stubbed, the test would not really test more than:

   verify(mockTimberlineEmployeeService).createTimberlineEmployee(null);

Cheers,
Szczepan

Russell Pitre

unread,
May 11, 2010, 8:41:39 AM5/11/10
to moc...@googlegroups.com
I'm using the latest and greatest 1.8.4.

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.4</version>
<scope>test</scope>
</dependency>


So is it safe to say that as a rule of thumb we shouldn't pass mocks
into method calls that are being verified? No big deal either way, I
just want to confirm this behavior.

verify(mockTimberlineEmployeeService).createTimberlineEmployee(mockEvent);


-Russ

Kristofer Karlsson

unread,
May 11, 2010, 9:00:28 AM5/11/10
to moc...@googlegroups.com
I am not actually sure why the verify gets confused by other invocations on mock objects, since it shouldn't depend on mock invocation ordering.

I can understand it for stubbing, since when(...).thenReturn(...) actually matches the latest mock invocation, but verify gets the actual mock object passed in instead.

szczepiq

unread,
May 11, 2010, 10:23:09 AM5/11/10
to moc...@googlegroups.com
>So is it safe to say that as a rule of thumb we shouldn't pass mocks
>into method calls that are being verified?  No big deal either way, I
>just want to confirm this behavior.

It's slightly different: you should avoid passing results of inlined execution of method on the same mock (nicely put, doesn't it :D ?).

//good:
verify(mockTimberlineEmployeeService).createTimberlineEmployee(mockEvent);

//not so good:
verify(mockTimberlineEmployeeService).createTimberlineEmployee(mockTimberlineEmployeeService.createMockEvent());

However.... I have just realized there is a bug in Mockito - thanks for reporting!

http://code.google.com/p/mockito/issues/detail?id=138

Until the bug is fixed avoid verifying on arguments that are produced by mocks in the same line.

Cheers,
Szczepan
Reply all
Reply to author
Forward
0 new messages