Tuesday, February 15, 2011

Unitils: @InjectIntoByType and @TestedObject

@TestedObject
AlertService alertService = new AlertService();

@InjectIntoByType(target="alertService")
Mock mockSchedulerService;
@InjectIntoByType(target="alertService")
Mock mockMessageService;

the target="alertService" is not required, since there is only one TestedObject.

instead of passing the Mock objects in the AlertService constructor, we can simply inject them using InjectIntoByType.

What I love is that no XML is required. XML sucks because it's too loosely connected with code.

See AlertServiceAnnotatedTest in http://javatoolsforweblogic.googlecode.com/files/TestUnitilsV2.zip for a working example.

Again, it's a pain to get all the required jars in the classpath... welcome to Java world :o(

No comments: