Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

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

As with other articles focused on the Mockito framework (like 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 Mockito can be used for mocking final classes and methods, it needs to be configured.

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 Mockito is properly configured, a final method can be mocked like any other:

@Test
public void whenMockFinalMethodMockWorks() {

    MyList myList = new MyList();

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

    assertNotEquals(mock.finalMethod(), 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);

    assertNotEquals(mock.size(), 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 tutorial, 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.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Vinicius
Vinicius
2 years ago

Hello baeldung.

Looks like the code is not available at github yet.

Anyways, thanks for sharing this mockito feature.

Vinicius
Vinicius
2 years ago

Hello, me again.

I would say that the code is indeed available, but the URL you provided does not match.
Also, the correct directory to put the file is mockito-extensions (like the code hosted at github) instead of mockito/extensions (like the article states).

Again, very useful content.
Thanks.

Grzegorz Piwowarek
2 years ago
Reply to  Vinicius

Fixed, we’re moving some modules around and missed some articles when updating. Thanks for pointing that out!

Bay Batu
Bay Batu
2 years ago

Example’s Github link gives 404. PowerMockito can also be used for mocking final classes without extra configuration.

Comments are closed on this article!