I've finally started using EasyMock with the HiveMind testing ... that's a huge amount of power in a tiny, little package!
If you haven't heard of this, the idea is that you can create mock implementations of services easily. First you create your control, and obtain the mock from it.
Next, you "train" your mock object, by invoking methods on it. The mock object and the control work together to remember the order of methods you invoke, and the argument values passed in. You use the control to specify return values.
Finally, you use the control to replay the mock and then test like it a real object.
Here's an example for the HiveMind suite:
public void testSetModuleRule() { MockControl control = MockControl.createStrictControl(SchemaProcessor.class); SchemaProcessor p = (SchemaProcessor) control.getMock(); Module m = new ModuleImpl(); Target t = new Target(); p.peek(); control.setReturnValue(t); p.getContributingModule(); control.setReturnValue(m); control.replay(); SetModuleRule rule = new SetModuleRule(); rule.setPropertyName("module"); rule.begin(p, null); assertSame(m, t.getModule()); control.verify(); }
Here I'm testing a SetModuleRule, which is dependent on the SchemaProcessor. SchemaProcessor are complex to create, and tied into the whole framework ... a lot of work for the two methods that the SetModuleRule will invoke on it!
This is great stuff, because it lets me easily mock up parts of the framework that are normally pretty inaccessible. Some of my tests use two or three mock/control pairs. This is still a big improvement over my existing approach, which is to feed a HiveMind module descriptor into the framework and test that it does the right thing. That's important, but its more of an integration test than a unit test ... it can be hard to tell precisely what failed.
No comments:
Post a Comment