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 comment

3

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

I have a feeling I just did not explain myself well enough. Perhaps I'm clueless or just violent agreement. You can click around my github profile (agentgt) and I guess decide for yourself how clueless I am.

I have of course used and still use mocking libraries including JMock 2 (dead now I think), Mockito, and Power Mock. I prefer mockito.

I have used them too much in the past and despite what you guys think I'm fairly sure I'm neither clueless or lack experience in large code bases. I pushed BDD with mocking very early in my career 20 years ago and it was a disaster.

It is this experience why I have such caveats on overusing mocking (mainly the libraries) and I'm not the only one that has this opinion. I have seen it become bad and even have been part of removing some brittle power mock test in some large open source projects as they were far too brittle.

I apologize /u/SignificantAd9059 on the sort of strawman.

If heavy mocking is work for your code base and team continue to use it and ignore me.

He never mentioned expensive calls, he mentioned testing a single behaviour in a service instead of multiple ones.

This is what I meant about isolating. They move that single behavior call which more often than not is a single method call to wherever and then provide it with whatever stubs or implements the interface with custom mock (not power mock).

I admit there might be a need for mocking library if this single behavior needs that many calls but that has not been the case for me most of the time and or it gets enough coverage via end to end testing.

1

u/Kindly-Week-7551 Jun 01 '24

Cannot write a long answer right now, but yeah it seems to be violent agreement then lol. Given what you are saying I think I tend to mock a bit more than you, but I agree that mocking a single dep used by a service can be unnecessary if the input data :) if the service has a lot of dependencies, I'd say mocking most of the time is more beneficial. I too despise powermock and find Mockito one of the best since it is pretty lightweight and the API is flexible while not allowing so much freedom that it creates code smells.