Verify with ArgumentCaptor is failing to match

2,155 views
Skip to first unread message

KARR, DAVID

unread,
Sep 27, 2012, 7:42:06 PM9/27/12
to moc...@googlegroups.com
I have a test that is using "verify()" where one of the parameters to the verified method is an ArgumentCaptor. The verified method also has two other parameters that I'm using "any..." expressions to match.

When I run the test, it's saying "Arguments are different".

This is essentially the error message, with much elided:
----------------
Argument(s) are different! Wanted:
jdbcTemplate.query(
<Capturing argument>,
<any>,
<any>
);
-> at ...Test.testGetMergedResults(...Test.java:49)
Actual invocation has different arguments:
jdbcTemplate.query(
PreparedStatementCreatorFactory.PreparedStatementCreatorImpl: sql=[select ... ]; parameters=[abc, def, abc, def, abc, def, abc, def, abc, def, abc, def],
...RowMapper@dac21
);
----------------

From the test method:

ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class);
verify(jdbcTemplate).query(queryCaptor.capture(), any(...RowMapper.class), anyMap());

I'm not sure what I'm seeing here.

Eric Lefevre-Ardant

unread,
Sep 28, 2012, 3:15:27 AM9/28/12
to moc...@googlegroups.com
I *think* that what's happening is that you asked to capture a String, but you actually got an instance of PreparedStatementCreatorImpl.


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


Brice Dutheil

unread,
Sep 28, 2012, 7:01:10 AM9/28/12
to moc...@googlegroups.com
Hi,

It seem like another method is called, one with a PreparedStatement, you check the signature of this method.

Also keep in mind that we use to say : don't mock types you don't own. In your case that would be JdbcTemplate as it's part of Spring. Instead you should write integration tests, with some database either real or inmemory. There's links on google that explain why mocking types you don't own this should be avoided.


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