Suppose I have a class Foothat is created only by an instance of the class Bar:
public Foo(Bar x) {
this.a = x.a();
this.b = x.b();
...
}
Now I would like to test Foo, assuming that the instance Barwith the desired state is difficult to create. As an additional limitation, the fields a, b, ...are declared final, therefore the settings for these fields are not available.
Perhaps it would be to create an additional constructor in Foo:
protected Foo(A a, B b, ...) {
this.a = a;
this.b = a;
...
}
This constructor is used only during testing, which I would declare in a comment for this constructor.
Question: Is it the smell of code?
Another solution I was thinking of was a mockery Bar. I wonder if this is the best practice in this case?