Reputation: 455
I'm unit testing my dao. I create a list, I add an object to my list and I tell mockito when my method is called, to return my list with a single object. However, when I look at what's returned from my dao method, it's an empty list. I'm not sure what I'm missing.
@InjectMocks
private Dao dao;
@Mock
private JdbcTemplate jdbcTemp;
@Test
public void testGetData() {
List<MyObj> list = new ArrayList<>();
MyObj myObj = new MyObj();
myObj.setMethod("method val");
list.add(myobj);
Mockito.when(jdbcTemp.query(anyString(), Mockito.any(PreparedStatementSetter.class),
Mockito.any(Dao.MyRowMapper.class))).thenReturn(list);
List<MyObj> res = dao.getData(param1, param2); // this is empty, instead of having a value of 1
Assertions.assertThat(res).isNotNull();
}
My Dao class:
public List<MyObj> getData(String arg1, String arg2) {
List<MyObj> list = new ArrayList<MyObj>();
try {
list.addAll(jdbcTemp.query(query, new PreparedStatementSetter() {
public void setValues(PreparedStatement pstmt) throws SQLException {
pstmt.setString(PARAM_ONE, arg1);
pstmt.setString(PARAM_TWO, arg2);
}
}, new MyRowMapper()));
} catch (Exception exp) {
}
return list;
}
Upvotes: 0
Views: 323
Reputation: 455
I actually made a mistake in describing the problem.
I had two jdbcTemplates in my dao.
So the way I resolved it is to use a @Qualifier("jdbcTemplate")
when creating a mock jdbctemplate
Upvotes: 1