2 ms·
class AccessCountedInteger { private final int value; private int accessCount = 0; AccessCountedInteger(int value) { this.v
by BlackFly 9y ago
class AccessCountedInteger {
private final int value;
private int accessCount = 0;
AccessCountedInteger(int value) {
this.value = value;
}
public int getValue() {
accessCount++;
return value;
}
public int getAccessCount() {
return accessCount;
}
}
This unit has side effects on the method getValue() that impact what is returned by getAccessCount(). While it is contrived, if you are making something following the builder pattern, you will in general have a lot of side effects from all of the methods called on the final build() method. This is quite amenable to unit testing.
- lmm 9y agoThat is not the kind of side effect that Haskellers would acknowledge as necessary (and I'd see it as a bad idea). We can call it a "side effect" but I think there's a qualitative difference between that and something like file I/O or async.
- mempko 9y agoWhy in the hell would you write a unit test in your contrived example? I mean look at it!