Mokito.when(jdbcTemplate.query(sql, new ParticipantMapper())).thenReturn(participantExistingList); I am using above line of code for Mocking jdbcTemplate but its not working. Can some one will help how to mock jdbcTemplate.
Use ArgumentMatchers for all Arguments, like this:
Mockito.when(jdbcTemplate.query(any(String.class), any(ParticipantMapper.class))) .thenReturn(participantExistingList); Depending on your wish to focus the interaction, you may use e.g. eq() for your sql String.
See here for JavaDoc.
Try doing this:
On your test class use:
@Mock JdbcTemplate jdbcTemplate; Then try:
Mokito.when(jdbcTemplate.query(sql, new ParticipantMapper())).thenReturn(participantExistingList); If it still fails, try:
doReturn(participantExistingList).when(jdbcTemplate).query(sql, new ParticipantMapper()); Hope this helps
Its important to see the order of your parameters inside query() method.
In my case, I wanted to mock for the following line:
List<String> someList = jdbcTemplate.query(SQL_STRING,new Object[] { Id }, new MyCustomMapper()); So I mocked it the following way, taking care of the order of parameters passed
when(jdbcTemplate.query(any(String.class),(Object[]) anyVararg(),any(MyCustomMapper.class))).thenReturn(myList);
Mockito.any(ParticipantMapper.class)instead ofnew ParticipantMapper()