r/java • • Jun 01 '24

What java technology (library, framework, feature) would not recommend and why?

165 Upvotes

464 comments sorted by

View all comments

Show parent comments

14

u/agentoutlier Jun 01 '24

I will just add that "mocking" especially using a library (and especially the one mentioned) should be the last resort after you have eliminated all other options.

I find custom built mocks like Spring's Servlet Mocks acceptable but still rather use the real thing.

17

u/SignificantAd9059 Jun 01 '24

It really depends on what you are mocking. It’s perfectly acceptable to mock services that you are not directly testing. Otherwise your making an integration test which is also useful but definitely not the same thing

-6

u/agentoutlier Jun 01 '24 edited Jun 01 '24

My question is why can't you isolate that part that isn't making the expensive call? (edit emphasis added as that isn't answered for the downvoters).

Like you don't need to pull in a mocking library to just override a method that does the expensive call.

I'm not saying never mock as technically the above is sort of mocking just that it should be used as the last resort especially now...

Computers are fast as fuck now. Test Containers, docker now make it so much easier to get the real thing that it really should be a choice of last resort and as I mentioned in another comment the really shitty slow APIs that you have to mock are often the ones that you cannot depend on to have a reliable contract making Mocking even more unreliable.

Otherwise your making an integration test which is also useful but definitely not the same thing

Complete bull shit made up by TDD zealots. The line of integration testing and unit testing is so arbitrary. Should I start mocking java.util.List? EDIT find for the downvoters I understand that was too extreme so I will give a better example. If I make a call to the file system to load a file should that be an integration test? How about from the classpath like a resource load? People do this all the time and they put them in src/test/java. Should those be integration tests?

Besides testing really should be about testing behavior and thus the very best test do tend to be more end to end.

EDIT if the calls to the collaborator (and notice this OOP TDD.. this stuff doesn't really apply for other programming) are that expensive I argue you are not "isolating" enough which is supposed to be the hallmark of unit testing. e.g. your "units" should be very small (I don't always agree with this but if we are going to go down the whole integration vs unit we have to bring this up).

9

u/Kindly-Week-7551 Jun 01 '24 edited Jun 01 '24

Using strawmans like "should I mock java.util.List" really shows that you did not even consider his point. He never mentioned expensive calls, he mentioned testing a single behaviour in a service instead of multiple ones. If you depend on a service that is already heavily covered in terms of unit testing, you do not really want to spend time testing it once more just because it is a dependency of the service you are testing. Mocking is fine when unit testing

→ More replies (10)