Issue with Spring JDBC ParameterizedRowMapper.

4,615 views
Skip to first unread message

Harish Kaparwan

unread,
Dec 3, 2015, 4:49:33 AM12/3/15
to mockito
Hi All

Can anyone please help me for mockito  example with Spring JDBC ParameterizedRowMapper class. In Mockito when we try to mock jdbctemplate.query( with ParameterizedRowMapper )  . It does not  mock and always return list.size()=0. 




In my code sample I want to return list object.

Here is my Test Class Code :- 
  •   List returnlist = new Arraylist();
             returnlist.add("DummyUser");

  •    mockito.when(jdbcTemplate.query(sql, ParameterizedRowMapper.class, args)).thenreturn(returnlist)

Here is my Java  code :- 
  •     List expectedResultList=jdbcTemplate.query(sql, new ParameterizedRowMapper({ // code for maprow }) , args)

Expected Output:-    String userName=expectedResultList.get(0);  
                                  userName value should return  "DummyUser".


KARR, DAVID

unread,
Dec 3, 2015, 12:05:42 PM12/3/15
to moc...@googlegroups.com

First, you don’t show that you have mocked the “jdbcTemplate” object.  That has to be a mock object.

 

Second, Mockito will do “parameter matching” to determine if the function call specified in the “when” clause will result in the specified return value.  I suggest you look at parameter matchers.  It’s not clear from this what you “need” to match for your test.  I also wonder how this could have compiled, as your test code is passing a Class object for the second parameter, but your CUT is passing a ParameterizedRowMapper.

 

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/d6102bff-f9d6-4461-960f-6687d350dc0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Harish Kaparwan

unread,
Dec 11, 2015, 7:21:34 AM12/11/15
to mockito
Hi David,

Thanks for your reply.

I have written a pseudo code to summarize my question. I can not copy actual code here.

My question is here it does not match any value inside of ParameterizedRowMapper in AccountingDao.java 

Mockito ignore everything when we use ParameterizedRowMapper with jdbctemplate.




***************************************************************************
private AccountingDao accountingDao;

 
  private SimpleJdbcTemplate jdbcTemplate;
  
List expectedOutputListObj = new Arraylist();
  returnlist.add("DummyUser");

@Test
    @SuppressWarnings("unchecked")
    public void testDataSourcMockList() throws SQLException {

        accountingDao = new AccountingDaoImpl();
 
// Below line will give me mock 'jdbcTemplate'. I can also use @Mock private JdbcTemplate jdbcTemplate;

jdbcTemplate=PrepareMockJdbcTemplate(DS_APP)

((AccountingDaoImpl) accountingDao).setTestingJdbcTemplate(jdbcTemplate);
 
MapSqlParameterSource mapSqlParameterSource= new MapSqlParameterSource();
mapSqlParameterSource.addValue("accountId","1234567");

String SQL=" Select * from account where accountId =: accountId";    
  ParameterizedRowMapper paramRowMapper= new ParameterizedRowMapper<Account>(){
public Account mapRow(Resultset rs,int row) {
// mock resultset
//mock account object
}
}

when(jdbcTemplate.query(eq(SQL), any(ParameterizedRowMapper.class) ,refEq(mapSqlParameterSource)).thenReturn(expectedOutputListObj);

        List dbListObj=accountingDao.getAccountList(SQL, paramRowMapper, mapSqlParameterSource)

assertTrue(dbListObj.size() >0); // Test Fails
                
    }
***************************************************************************

tora...@gmail.com

unread,
Mar 7, 2016, 5:34:04 AM3/7/16
to mockito
Hi,

I am getting into similar issues with a different method in NamedParameterJdbcTemplate. Here is the full code.
I am getting compile error in line 26. But I have a dummy implementation of the method in line 29 to show the method with those parameter exists.

Any ideas?


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

@RunWith(MockitoJUnitRunner.class)
public class MyMockTest {

@InjectMocks
SearchDAO dao;

@Mock
NamedParameterJdbcTemplate jdbcTemplate;

@Test
public void testSearch() {

Mockito.when(jdbcTemplate.query(Mockito.any(String.class),
Mockito.any(HashMap.class), Mockito.any(RowMapper.class)));

jdbcTemplate.query("", new HashMap(), new RowMapper() {

@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
// TODO Auto-generated method stub
return null;
}
});
Reply all
Reply to author
Forward
0 new messages