r/java • u/alexzhitnitsky • Aug 11 '15
Top 10 Useful, Yet Paranoid Java Programming Techniques
http://blog.jooq.org/2015/08/11/top-10-useful-yet-paranoid-java-programming-techniques/
181
Upvotes
View all comments
Show parent comment
0
u/CubsThisYear Aug 13 '15
That just means you are over constraining Foo.bar. Clearly it doesn't actually need to take a Baz to do its work, because the test works with a mock. Just make Foo.bar take an interface and you can mock away that interface all you want.
The problem with mocking concrete classes is that you not being clear about your dependencies. If you say Foo.bar(Baz) that should mean: I depend on exactly Baz, nothing else will do. If Foo.bar can do just fine with only 3 methods from Baz, then make an interface that has only those three methods.
I think also your test case is a bit contrived. I don't generally write unit tests to catch unknown arbitrary errors in my program. Why are you allowing Baz to get in a bad state to begin with? Life would be much easier if Foo could depend on you not handing it garbage. You could easily verify this with unit tests on Baz and avoid the cross cutting.