You could pass a dummy output stream to the superclass constructor, such as the NullOutputStream from Apache commons-io, which is an OutputStream implementation that silently discards all data written to it.
Alternatively, you could consider declaring your PersonDumper
to take an instance of Appendable
instead. PrintStream
implements Appendable
so this won't change your code in the normal case, but for testing you could pass in a StringBuilder
or similar instead.
class PersonDumper {
// attributes
public PersonDumper(Appendable output, Set<Person> persons) {
// do stuff
}
public dump() {
for(Person p : persons) {
// String.valueOf rather than .toString to be null-safe
output.append(String.valueOf(p));
output.append(",");
}
}
}