java - Refactoring to test -
i have piece of code equivalent following.
public class concretethread extends otherthread { private daofirst firstdao; private daosecond seconddao; private transformservice transformservice; private networkservice networkservice; public concretethread(daofirst first, daosecond second, transformservice service1, networkservice service2) { firstdao = first; seconddao = second; transformservice = service1; networkservice = service2; } public future go() { results r1 = firstdao.getresults(); mycallable c1 = new mycallable(r1); return super.getthreadpool().submit(c1); } private class mycallable implements callable { private results result; private long count; private mycallable(results r) { this.result = r; this.count = new long(0); } public long call() { singleton transactions = singleton.getinstance(); try { transactions.begin(); while(result != null) { transformed t = transformservice.transform(r1); networkservice.sendsomewhere(t); count = count += result.size(); seconddao.persist(result); result = firstdao.getnext(result); } } catch (exception e) { e.printstacktrace(); } { transactions.end(); } } }
neither of these classes (the inner or outer) have unit tests, , turns out inner class, mycallable
, has bug in it. in simplified version of code i've given above, bug isn't present.
so, lets assume decide fix bug, , implement unit tests mycallable
. question this; how go writing unit tests mycallable
inner class?
my own solution first refactor mycallable
, concretethread
. mycallable
made public class in own file, , concretethread
passes in daos, services , singleton constructor arguments mycallable
, rather relying on inner-class' access it's private variables.
i used easymock heavily in unit tests mock dependencies , verify being called in manner expected.
a consequence of code mycallable
larger was. no longer has access private variables in concretethread
, concretethread
must pass them in arguments in constructor, , mycallable
sets them private variables.
do think wrong approach? perhaps in performing sort of refactoring have broken encapsulation , added unnecessary boilerplate code base? have used reflection in tests instead?
a consequence of code mycallable larger was. no longer has access private variables in concretethread, concretethread must pass them in arguments in constructor, , mycallable sets them private variables.
that's consequence, mycallable no longer dependent on changes in concretethread.
i think question , answer quite subjective, think followed solid principle in refactoring (which thing).
and if can, make mycallable package protected, not public :)
Comments
Post a Comment