Jmockit/Spring mocked dependency still calls the Real dependency -


so i've been stuck on problem day.

i'm testing class of type jdbcsupportdao in spring 3.2. problem self-explanatory if read code, briefly summarize:

i use @mocked annotation on jdbctemplate mock querying database. problem is, after writing expectations block, actual jdbctemplate method still being called, jmockit apparently not entering in equation @ all.

the following unit test fails:

/*@runwith(springjunit4classrunner.class)*/ @runwith(jmockit.class) @contextconfiguration(locations={"classpath:studentaggregatereport-servlet.xml", "classpath:applicationcontext-hibernate.xml"}) public class jdbcssodaotest   extends abstracttransactionaljunit4springcontexttests {      @mocked     jdbctemplate jdbctemplate;      list<string> unamelist;       ssodao ssodao;      string dummy_alcid = "yattayattayatta";      @before     public void constructdao() {         this.ssodao = new jdbcssodao();         ((jdbcssodao) ssodao).setjdbctemplate(jdbctemplate);     }      @test     public void testgetunamefromalcid() {         unamelist = new arraylist<string>() {{             add("peepee");         }};         //((jdbcssodao) ssodao).setjdbctemplate(jdbctemplate);         new expectations() {{             jdbctemplate.query(anystring, (resultsetextractor<string>)any); result = unamelist;         }};          string uname = ssodao.getunamefromalcid(dummy_alcid);          assertnotnull(uname);     }  } 

and here code class being tested:

public class jdbcssodao extends jdbcdaosupport implements ssodao {      @override     public string getunamefromalcid(string alcid) {         string sql = ssosqlutil.creategetunamebyalcidsql(alcid);         logger.debug(sql);         list<string> resultlst = getjdbctemplate().query(sql, new rowmapper<string>() {             public string maprow(resultset rs, int rownum) throws sqlexception {                 return rs.getstring(1);             }         });         if(resultlst.isempty()) return null;         return resultlst.get(0);     }  } 

please :(

sweet mother of god..

apparently, have cast parameters of mocked methods exact type used in call. fixed me:

    new expectations() {{         jdbctemplate.query(anystring, (rowmapper<string>)any); result = unamelist;     }}; 

Comments