So in chapter 13 Intro to Unit Testing, haveFunAtDuckPond
is defined as:
public void haveFunAtDuckPond(DuckPond duckPond) {
System.out.println("Having fun at the duck pond");
duckPond.haveFun();
}
in order to illustrate the difficulty of testing impure functions.
Then, at the beginning of chapter 14 Advanced Unit Testing, you have this implementation of haveFunAtDuckPond
:
public int haveFunAtDuckPond(Duck d, int numFeedings) {
if (d == null || numFeedings <= 0) { return 0; }
int amountOfFun = 0;
for (int j=0; j < numFeedings; j++) {
amountOfFun++;
d.feed();
d.quack();
}
return amountOfFun;
}
This one has a return value in addition to some side effects.
THEN, later in chapter 14 in section 14.3 on Mocks and Verification, it says:
"Yes, yes, this is all fine," I can hear you saying, "but you didn't answer the original question! We are still dependent on asserting on a value that is returned from a method, and thus won't be able to test methods without a return value!" Remember the method that we wanted to test from earlier in the chapter:
public class Person {
public void haveFunAtDuckPond(DuckPond duckPond) {
duckPond.haveFun();
}
}
There is no return value and thus nothing on which to assert.
So for one thing, this implementation is not from earlier in this chapter, it's from earlier in the book or from the previous chapter.
For another thing, the intervening reimplementation of haveFunAtDuckPond
between this reference and its antecedent is confusing... which one am I supposed to be remembering? Why are there 2?
I'd consider making them be two separate concepts entirely and only have one implementation of haveFunAtDuckPond
in the book, even though in the real world there are multiple ways one could have fun at a duck pond (not stepping in duck poop is my definition of having fun at a duck pond ;)