Explain Codes LogoExplain Codes Logo

How to use Mockito with JUnit 5?

java
mockito
junit-5
test-injection
Nikita BarsukovbyNikita Barsukov·Jan 13, 2025
TLDR

Swiftly integrate Mockito with JUnit 5 by including the mockito-junit-jupiter dependency. Utilize Mockito annotations within JUnit 5 tests by using @ExtendWith(MockitoExtension.class). Here’s the core essence brought out:

import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) class QuickTest { @Mock MyComponent component; // Your imaginary friend in test land @Test void verifyComponentAction() { when(component.act()).thenReturn("Done"); // When called upon, always be 'Done' assertEquals("Done", component.act()); // You expect your friend to be 'Done' verify(component).act(); // Did you really do as promised? } }

Create mocks without sweat using @Mock, simulate behaviors with when().thenReturn(), and verify interactions using verify(), encapsulating a smooth Mockito + JUnit 5 workflow.

Migration story: From JUnit 4 to JUnit 5

While transitioning from JUnit 4 to JUnit 5, realize that JUnit 4 rules and runners do not play along with JUnit 5. Invoke the @ExtendWith annotation as a replacement to JUnit 4's @RunWith to marry Mockito's capabilities with JUnit 5. Ascertain that your mocks are properly instantiated and avoid the manual mock creations to take full advantage of JUnit 5's extensions.

Mastering the art of Mock Instantiation

Your choice of injection can make a world of difference. If you prefer constructor injection over field injection with @Mock, consider applying @InjectMocks providing Mockito the controls to instantiate your test subjects resulting in cleaner and more simplified test code.

Customize your testscape!

The MockitoExtension provides a plentiful functionality. But if you desire for more additions, consider rolling out your own custom extension implementing the appropriate JUnit 5 callbacks (BeforeAllCallback, BeforeEachCallback, etc.). This equips you with an extra level of control and customizability over your test setup and behaviour.

Structuring your Test

Design your tests with a sharp focus on isolation. Dodge the lure of creating an inheritance hierarchy which can wreak havoc, resulting in brittle tests. Channel the power of the JUnit 5 extensions to keep your tests succinct and maintainable.

Kotlin's rendezvous with Mockito

If you're dealing with Kotlin, make good use of the lateinit modifier with @Mock for lazy initialization of objects in your JUnit 5 tests. Keep your Kotlin stylish and expressive!

The JUnit 5 test construct

Always harbor the goal of achieving test isolation. Truncate your tests' coupling to a base class configuration. Instead, anoint the extensions for crafting flexible and sleek test scenarios. Lastly, always ensure the correct usage of @ExtendWith and @Mock for not-so-surprising surprises with mock injection.