unit testing - How to mock java methods of superclass using powermock or any mock tool? -
super class
public class basicdao { public object createquery() { return new object(); } }
implementation
public class mydao implements basicdao { public object getmydata() { object obj = createquery(); // more code... return ...; } }
i need test getmydata()
method , want mock/suppress createquery()
method cos fail on test env.
thanks!
don't tests way. called partial mocking , wrong, means code have been written not using object oriented design. 1 should favor composition on inheritance.
nevertheless dao object represents boundary of system, i.e.
- one side object oriented
- one side relational
in case should write integration tests,
- insert data in db, actual instance or in-memory instance (h2, hsqldb, ...)
- prepare data, there's plenty possibilities here (dbunit, dbsetup, ...)
- configure , run dao tests on database
it may require more setup , more time run better in long run.
- more robust mapping testing
- easier change technical implementation, without changing tests
- run against database, can detect configuration issues/challenges sooner, , make sure don't break 3 years later
also answer of @user3386493 technically correct i'd suggest use kind of stubbing spies (otherwise actual method code executed) :
mydao daospy = mockito.spy(new mydao()); doreturn(new object()).when(daospy).createquery();
Comments
Post a Comment