'How can I mock dependencies which aren't interfaces but subclasses?
I'm not sure if my design is simply stupid, or there's a good way to make it work. Consider the following classes:
public abstract class CheckBase {
protected Collector collector;
public void addData(String data) {
collector.addData(data);
}
public void setCollector(Collector collector) {
this.collector = collector;
}
}
interface Collector {
void addData(String data);
}
final class DogCheck extends CheckBase {
public void someCheck() {
// check
addData("SomeData");
}
}
class ClassWithDependencies {
private final DogCheck dogCheck;
public ClassWithDependencies(DogCheck dogCheck) {
this.dogCheck = dogCheck;
}
}
Now I want to test something in ClassWithDependencies
, which does not need DogCheck
, or should have a mock version of it.
How can I do this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|