Why Mockito’s @InjectMocks is evil

Why Mockito’s @InjectMocks is evil:
https://lnkd.in/gDK2ktX

TLDR: It fails to inject silently

@InjectMocks will try to satisfy a dependency with a @Mock by adding to the system under test via constructor, setter, or property.

If any of those do not succeed, it will fail silently, and you won’t know why you have a NullPointerException, for example.

Better to explicitly declare and add your mocks with Mockito.mock() instead of relying on @InjectMocks to autowire your dependencies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s