r/java • • Nov 26 '20

Java libraries I like

https://sizovs.net/2020/11/24/java-libraries-i-like/
285 Upvotes

62 comments sorted by

View all comments

6

u/kag0 Nov 26 '20

Interesting to see mockito and vavr didn't make the list

11

u/zalpha314 Nov 26 '20

I think that we're starting to learn, as a community, that mocks are a slippery slope that can cause more problems than they're worth, especially if used in excess.

I used to use mocking libraries, but I'm now arranging my architecture in a way that they aren't necessary anymore.

5

u/TipsyRootNode Nov 26 '20

Can you share how? I still use mocks for external services and possible failure scenarios. What approach are you taking?

6

u/zalpha314 Nov 26 '20

I use what you could call Hexagonal Architecture. Your core business logic has a class, but then any inbound or outbound calls go through an interface that's passed in. For testing, you can pass in interfaces that are either:

- mocked (mockito, easymock, etc)

- faked (create your own manual, simplistic implementation)

- emulated (moto, dynamodb-local, h2, mock-aws-java-sdk, fake embedded server etc.)

Any of these options can be used to implement or instrument your adapter classes.
There's nothing wrong with mocking if it's done well, but I find it can easily be abused, so I try to avoid it.

1

u/[deleted] Nov 27 '20

So you still use Mockito? But only for interfaces? Thats the intended use.

1

u/zalpha314 Nov 27 '20

No, I don't actually use it; I just listed it as an option for completeness.

2

u/RobinHoudini Nov 27 '20

Isn't mockito just a quick way to create fake, simplistic implementation? IMHO it's less work and less chances of faking the implementation incorrectly.

3

u/zalpha314 Nov 27 '20

I think that's the intention. But in practice, I find it harder to maintain and less predictable. But maybe I'm just not very good with it.