java - How to test runnable which executed another runnable -


i have runnable 1 of parameter delegator taskexecutor execute runnable:

@override public void run() {     try {         dotask(messageid);     } catch (exception e) {         count++;         if (count < 4) {             delegatedtransactionalasynctaskexecutor.execute(this);         } else {             delegatedtransactionalasynctaskexecutor.execute(getonexceederrortask(messageid));         }         throw new runtimeexception(e);     } } 

how should test ?

it seems delegatedtransactionalasynctaskexecutor field in class.

in order ensure can test it, have use dependency injection, this:

class undertest {   private final whatever delegatedtransactionalasynctaskexecutor;   undertest(whatever delegatedtransactionalasynctaskexecutor) {     this.delegatedtransactionalasynctaskexecutor = delegatedtransactionalasynctaskexecutor;   ... 

and now, can use mocking frameworks create objects of whatever class. mocks allow specify method calls expect happen; , then, can verify later on calls took place.

in other words: prepare mock; invoke run() ... , afterwards check calls looking happened. , of course whole thing work, must able inject mocks "class under test".


Comments