Assume I have a class Foo
which is only instantiated with an instance of class Bar
:
public Foo(Bar x) {
this.a = x.a();
this.b = x.b();
...
}
Now I would like to test Foo
, further assuming an instance of Bar
with the desired state is difficult to create. As an additional constraint, the fields a, b, ...
are declared as final, so setters for this fields are not available.
A possibility would be to create an additional constructor in Foo:
protected Foo(A a, B b, ...) {
this.a = a;
this.b = a;
...
}
This constructor is only used during testing, which I would declare in the comment for this constructor.
Question: Is this a code smell?
Another solution I was thinking of was mocking Bar
. Wonder if its the best practice in this case?
Mocking Bar is more likely to be considered best practice. You should be able to create a MockBar so you can do
Foo foo = new Foo(new MockBar(a, b));
I would probably consider that poor form, but if it works, it works. A preferable solution would be making a TestBar
class that is a sub-class of Bar
and overrides the methods in Bar
. I guess that's pretty much mocking the object, as you alluded to in your question. It's a lot cleaner and allows you to define a different Bar
implementation that can be included by your test driver.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With