i have interface shoppinglistdao below.
public interface shoppinglistdao extends genericdao<object, string> { public list<shoppinglist> getshoppinglist(department department) throws shoppinglistdaoexception; } and it's implementation dao class below one.
public class shoppinglistdaoimpl extends genericcustomdao<object, string> implements shoppinglistdao { //....... public list<shoppinglist> getshoppinglist(department department) throws shoppinglistdaoexception { try { ds = getdatasource(); connection = ds.getconnection(); callablestatment = connection.preparecall(shopping_list_qry1); callablestatment.setstring(1, department.getdistributornumber()); //...... callablestatment.registeroutparameter(4, oracletypes.cursor); callablestatment.execute(); resultset= (resultset) callablestatment.getobject(4); while(resultset.next()) { //....... } } catch (sqlexception e) { e.printstacktrace(); throw new shoppinglistdaoexception(e); } catch (exception e) { e.printstacktrace(); throw new shoppinglistdaoexception(e); }finally{ //...... } } return shoppinglist; } now have requirement test implemented dao class using mock db objects.i searched through powermock/easymock documentation guess of api methods provides me objects provide me dummy implementation class of dao interface.
is there way can create mock object of connection (assuming don't have physical database access) , can run subsequent code provided in
shoppinglistdaoimplclass have use mocking code coverage purpose?if there way can make
callablestatement.execute()return dummy data or exception(withour physical database access) can check in junit test cases?
i quite new mocking framework ,so may requirements unrealistic. information helpful.
with mock frameworks easymock, powermock or mockito, can mock everything. not recommend mock connection.
in case, use in-memory database hsqldb test dao against real db db runs in-memory. way, test not dependent of external resources , can run on environment.
by testing code against database, strictly not unit testing anymore. though think acceptable tradeoff.
Comments
Post a Comment