Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


1. Overview

In this quick tutorial, we’ll show how to integrate Mockito with the JUnit 5 extension model. To learn more about the JUnit 5 extension model, have a look at this article.

First, we’ll show how to create an extension that automatically creates mock objects for any class attribute or method parameter annotated with @Mock.

Then we’ll use our Mockito extension in a JUnit 5 test class.

2. Maven Dependencies

2.1. Required Dependencies

Let’s add the JUnit 5 (jupiter) and mockito dependencies to our pom.xml:


2.2. Surefire Plugin

Let’s also configure the Maven Surefire plugin to run our test classes using the new JUnit platform launcher:


The latest versions of junit-jupiter-engine, mockito-core and junit-platform-surefire-provider can be downloaded from Maven Central.

3. Mockito Extension

Mockito provides an implementation for JUnit5 extensions in the library – mockito-junit-jupiter.

We'll include this dependency in our pom.xml:


4. Building the Test Class

Let’s build our test class and attach the Mockito extension to it:

class UserServiceUnitTest {

    UserService userService;

    // ...

We can use the @Mock annotation to inject a mock for an instance variable that we can use anywhere in the test class:

@Mock UserRepository userRepository;

Also, we can inject mock objects into method parameters:

void init(@Mock SettingRepository settingRepository) {
    userService = new DefaultUserService(userRepository, settingRepository, mailClient);

Please note the use of Mockito.lenient() here. Mockito throws an UnsupportedStubbingException when an initialized mock is not called by one of the test methods during execution. We can avoid this strict stub checking by using this method when initializing the mocks.

We can even inject a mock object into a test method parameter:

void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) {
    // Given
    user = new User("Jerry", 12);
    when(userRepository.insert(any(User.class))).then(new Answer<User>() {
        int sequence = 1;
        public User answer(InvocationOnMock invocation) throws Throwable {
            User user = (User) invocation.getArgument(0);
            return user;

    userService = new DefaultUserService(userRepository, settingRepository, mailClient);

    // When
    User insertedUser = userService.register(user);
    // Then

Note that the MailClient mock we inject as a test parameter will NOT be the same instance we injected in the init method.

5. Conclusion

Junit 5 has provided a nice model for the extension. We demonstrated a simple Mockito extension that simplified our mock creation logic.

All the code used in this article can be found on our GitHub project.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


Junit footer banner
Comments are closed on this article!