你会创建一个 setterotherStuff
并且在你打电话之前RunOtherStuff()
你会打电话setOtherStuff(myFake)
public class Stuff {
private OtherStuff otherStuff;
public void setOtherStuff(OtherStuff otherStuff) {
this.otherStuff = otherStuff;
}
...
public void RunOtherStuff() {
otherStuff.run();
}
}
那么你的测试可以这样写:
private Stuff stuff;
private boolean runWasCalled;
public void setUp() {
stuff = new Stuff();
stuff.setOtherStuff(new OtherStuff() {
public void run() {
runWasCalled = true;
}
});
}
public void testThatOtherStuffRunMethodIsCalled() {
stuff.RunOtherStuff();
assertTrue(runWasCalled);
}