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

-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

-3

u/agentoutlier 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

Completely ignoring how I said "last resort" is why I had to go there. Obviously I'm not saying it should never be done. I'm saying rarely does it require a library.

Do you consider overriding a method of a class that makes an expensive call "mocking"?

Mocking is not entirely well defined as some would not call that mocking.

I'm in complete agreement replacing some call with a cheaper quick call is sometimes needed. What I'm not OK is doing that as some strategy for testing all the time and or using complicated library like Power Mock.

As well as then calling somethings integration or unit testing because of doing the above.

There are fast tests and there are slow tests is the reality. The fast tests people like to call unit tests and the slow ones integration but the reality is we are integrating all the time.

If you depend on a service that is already heavily covered in terms of unit testing, you do not really want to spend time resting it once more just because it is a dependency of the service you are testin

Why is that happening? Shouldn't the code be separated?

5

u/Kindly-Week-7551 Jun 01 '24

Why is that happening? Shouldn't the code be separated?

yeah, you're either purposely missing the point or clueless. I agree with you though, having no interaction between components of your system guarantees that nothing ever happens, no need to separate emptiness.

→ More replies (2)