Core Java

Understanding Mockito Core vs Mockito Inline

Mockito is a popular mocking framework for unit testing in Java, allowing us to create mock objects and define their behaviour. As the framework has evolved, the Mockito community introduced Mockito Inline, which comes with features that extend the capabilities of the traditional Mockito Core. In this article on Mockito Core vs Mockito Inline, we will explore the key differences between Mockito Core and Mockito Inline, focusing on features such as mocking final classes, fields, as well as static methods.

1. Introduction to Mockito Core

Mockito Core is the standard version of the Mockito framework that is widely used for creating mocks, spies, and stubs in tests. It is designed primarily for testing classes without the need for additional dependencies or special configurations. Before version 5.0.0, Mockito Core did not support mocking final classes, fields, or static methods. These limitations were addressed by the Mockito Inline extension.

With version 5.0.0, Mockito introduced a major change by switching the default MockMaker interface to the one used by Mockito Inline, enabling support for mocking constructors, static methods, and final classes directly within Mockito Core itself. Before this update, Mockito Core was primarily focused on simpler mocking scenarios without these advanced features.

2. What is Mockito Inline?

Before Mockito version 5.0.0, Mockito Inline was an extension that specifically allowed mocking of final classes, final methods, and static methods, features that were unavailable in the standard Mockito Core. This made it particularly useful for working with legacy code or libraries that heavily relied on these constructs.

With the release of version 5.0.0, Mockito Inline became the default behaviour for the framework, as Mockito switched to the mockito-inline MockMaker interface, allowing these advanced mocking capabilities out of the box. This change means we no longer need to explicitly configure Mockito Inline for these features.

3. Configuration

To use Mockito core, we need to add the following dependency to our pom.xml file:

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>5.14.1</version>
            <scope>test</scope>
        </dependency>

3.1 Mocking Final Classes

Mockito Core enables us to mock final classes, as shown in the following example:

public final class FinalService {

    public String getMessage() {
        return "Service Active!";
    }
}
public class FinalServiceTest {
    
    @Test
    public void testFinalServiceMock() {
        FinalService finalServiceMock = mock(FinalService.class);
        when(finalServiceMock.getMessage()).thenReturn("Mocked Service Message");
        assertEquals("Mocked Service Message", finalServiceMock.getMessage());

        System.out.println(finalServiceMock.getMessage()); 
    }    
}

In the example above, we leveraged Mockito to mock a final class. By Using mock(FinalService.class), we created a mock instance of the FinalService class, overriding its getMessage() method to return a custom string (“Mocked Service Message”).

3.2 Mocking a Final Field

We can mock a final field by using Mockito.when() to define the behaviour of the method that accesses the final field.

public class ServiceWithFinalField {

    private final String config = "Production Config";

    public String getConfig() {
        return config;
    }
}
public class ServiceWithFinalFieldTest {

    @Test
    public void testMockFinalField() {
        // Mock the class containing the final field
        ServiceWithFinalField serviceMock = mock(ServiceWithFinalField.class);

        // Define behavior for the method that accesses the final field
        when(serviceMock.getConfig()).thenReturn("Mocked Config");
        assertEquals("Mocked Config", serviceMock.getConfig());

        System.out.println(serviceMock.getConfig());
    }
}

In this example, the final field config in ServiceWithFinalField is indirectly mocked by overriding the behaviour of the getConfig() method.

3.3 Mocking Static Methods

Mockito also allows us to mock static methods like this:

public class StaticClass {

    public static String getStatus() {
        return "Running Smoothly!";
    }
}
public class StaticClassTest {

    @Test
    public void testStaticMethodMock() {
        try (MockedStatic<StaticClass> mockedStatic = mockStatic(StaticClass.class)) {
            mockedStatic.when(StaticClass::getStatus).thenReturn("Mocked Status");

            assertEquals("Mocked Status", StaticClass.getStatus());
            System.out.println(StaticClass.getStatus());
        }
    }
}

In this example, we showcased how Mockito allows us to mock static methods. By using mockStatic(StaticClass.class), we can override the behaviour of the getStatus() method to return a custom value (“Mocked Status”) instead of its default output.

4. Conclusion

In this article, we examined the differences between Mockito Core and Mockito Inline. Starting with Mockito version 5.0.0, Mockito has become widely used for standard unit testing, offering support for mocking final classes, final methods, and static methods.

5. Download the Source Code

This article explored the differences between Mockito Core vs Mockito Inline.

Download
You can download the full source code of this example here: mockito core vs mockito inline

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button