0

I am trying to mock a DAO implementation class that extends NamedParameterJdbcDaoSupport

This is how my DAO interface looks like:

public interface TestDAO{

List<String> search();

}

This is how my implementation class is:

    public class TestDAOImpl extends NamedParameterJdbcDaoSupport implements TestDAO {

    public List<String> search(){

    return getNamedParameterJdbcTemplate().query(mySQLQuery,myMapSqlParameterSource, myRowMapper);
    }
}

What I am trying to achieve is that write a mock test case to mock the behaviour of the call

getNamedParameterJdbcTemplate().query(mySQLQuery,myMapSqlParameterSource, myRowMapper)

So I want to implemented something like

when(getNamedParameterJdbcTemplate().query(mySQLQuery,myMapSqlParameterSource, myRowMapper)).thenReturn(whatIWantToReturn);

I am unable to find any information on this. Can someone guide me. How to do this?

public class TestDAOImplTestCase{

    @Test
    public void testSearch(){
        when(getNamedParameterJdbcTemplate().query(mySQLQuery,myMapSqlParameterSource, myRowMapper)).thenReturn(whatIWantToReturn); 
    } 
}
Aniks
  • 1,011
  • 3
  • 17
  • 29

1 Answers1

0

trust me, you don't want to do it. what you really want to do is do two tests:

  1. the easier one: mock the whole TestDao interface and unit test if rest of your code passes correct parameters to search and properly handles the result

  2. a bit harder one: integration test. take real implementation of TestDAOImpl, connect it to real database, prefill database with data, execute the search method and check if it returns correct answer

piotrek
  • 13,982
  • 13
  • 79
  • 165