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 end-of-day today:

>> GET ACCESS NOW

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

>> GET ACCESS NOW

1. Overview

In this short tutorial, we'll focus on how to mock final classes and methods using Mockito.

As with other articles focused on the Mockito framework (such as Mockito Verify, Mockito When/Then and Mockito's Mock Methods), we'll use the MyList class shown below as the collaborator in test cases.

We’ll add a new method for this tutorial:

public class MyList extends AbstractList {
    final public int finalMethod() {
        return 0;
    }
}

And we'll also extend it with a final subclass:

public final class FinalList extends MyList {

    @Override
    public int size() {
        return 1;
    }
}

2. Configure Mockito for Final Methods and Classes

Before we can use Mockito for mocking final classes and methods, we have to configure it.

We need to add a text file to the project's src/test/resources/mockito-extensions directory named org.mockito.plugins.MockMaker and add a single line of text:

mock-maker-inline

Mockito checks the extensions directory for configuration files when it is loaded. This file enables the mocking of final methods and classes.

3. Mock a Final Method

Once we've properly configured Mockito, we can mock a final method like any other:

@Test
public void whenMockFinalMethodMockWorks() {

    MyList myList = new MyList();

    MyList mock = mock(MyList.class);
    when(mock.finalMethod()).thenReturn(1);

    assertThat(mock.finalMethod()).isNotEqualTo(myList.finalMethod());
}

By creating a concrete instance and a mock instance of MyList, we can compare the values returned by both versions of finalMethod() and verify that the mock is called.

4. Mock a Final Class

Mocking a final class is just as easy as mocking any other class:

@Test
public void whenMockFinalClassMockWorks() {

    FinalList finalList = new FinalList();

    FinalList mock = mock(FinalList.class);
    when(mock.size()).thenReturn(2);

    assertThat(mock.size()).isNotEqualTo(finalList.size());
}

Similar to the test above, we create a concrete instance and a mock instance of our final class, mock a method and verify that the mocked instance behaves differently.

5. Conclusion

In this quick article, we covered how to mock final classes and methods with Mockito by using a Mockito extension.

The full examples, as always, can be found over on GitHub.

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

>> GET ACCESS NOW

Junit footer banner
4 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!