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 comment