r/java • • 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

76 comments sorted by

View all comments

Show parent comments

3

u/CubsThisYear Aug 12 '15

You should never be mocking non-abstract methods. If you need to mock something, then by definition you have two implementations of the same functionality so you should have an interface for that functionality.

If you start mocking non-abstract methods, that means you are modifying your objects just for the purpose of testing. This will ultimately make your tests either brittle or incomplete.

1

u/[deleted] Aug 12 '15

Unit tests are, by design, incomplete. They are trying to test a tiny portion of your code in isolation.

The goal of unit tests isn't to tell if your system as a whole works. It is to quickly isolate failures when they do happen.

0

u/CubsThisYear Aug 12 '15

They are incomplete by necessity, not by design. If you could test your entire system while still making it relatively easy to change, you certainly would, given infinite time.

But none of that justifies using mocks on concrete classes. If you need to mock an implemented method you've either violated SRP or you are pretending something isn't pluggable when it clearly needs to be (otherwise you wouldn't be plugging in the mock). Your code now looks like it operates on a single implementation but it really doesn't.

1

u/voronaam Aug 13 '15

They are indeed incomplete by design. Whole system test are called integration tests. Unit tests are supposed to test a small unit of work in isolation, just like it has been pointed out already.