Home > Uncategorized > Leveraging Mockito’s MockConstruction for Effective Unit Testing

Leveraging Mockito’s MockConstruction for Effective Unit Testing

Anastasios Antoniadis

Explore when and why mocking constructor calls is essential in unit testing, including handling external dependencies, complex initialization, side effects, and more. This guide delves into scenarios where mocking constructors enhances test isolation and reliability, especially in legacy code and third-party libraries, ensuring focused and efficient testing practices.


In software development, unit testing is a crucial practice ensuring individual units of source code work as expected. For Java developers, Mockito stands out as a popular mocking framework that simplifies the creation of testable code by simulating complex objects’ behavior. One of Mockito’s lesser-known yet powerful features is MockConstruction, introduced in Mockito 3.4.0. This feature significantly enhances the ability to mock object construction, paving the way for more comprehensive and reliable unit tests. This article delves into MockConstruction, explaining its importance, how to use it, and the benefits it brings to unit testing.

Understanding MockConstruction

Traditionally, mocking frameworks have allowed developers to mock interfaces and classes but fell short when it came to mocking the construction of objects. This limitation often led to cumbersome workarounds, especially when dealing with legacy code or third-party libraries where modifying the source code wasn’t an option.

MockConstruction bridges this gap by allowing developers to mock object creation, making it possible to control the behavior of newly created objects within the scope of a test. This capability is particularly useful when working with objects with side effects in their constructors or when the object’s construction involves complex initialization logic that you want to bypass in a unit test.

Mocking constructor calls becomes necessary in several scenarios during unit testing, especially when aiming for isolated and reliable tests. Here are some of the key situations where you might need to mock constructor calls:

  1. External Dependencies: When the class under test creates instances of external dependencies within its methods, and these dependencies are not injected through the constructor or setter methods. Mocking the constructor calls allows you to replace these external dependencies with mocks, ensuring that the unit test focuses solely on the logic within the class under test.
  2. Complex Initialization: If the object being constructed performs complex initialization in its constructor that is irrelevant to the test context or makes the test setup cumbersome. Mocking the constructor allows you to bypass this complex initialization, focusing the test on specific behaviors.
  3. Side Effects in Constructors: When the construction of an object has side effects that you want to avoid in a test environment, such as network calls, database operations, or file system interactions. Mocking the constructor prevents these side effects from affecting your test outcome.
  4. Uncontrollable or Non-Deterministic Behavior: Some objects may exhibit difficult behavior to control or predict, such as generating unique IDs, timestamps, or relying on the current system state. Mocking these objects’ construction can help achieve determinism and controllability in tests.
  5. Testing Legacy Code: Legacy code often lacks dependency injection, making it hard to substitute dependencies with mocks or stubs. Mocking constructors can be an effective strategy to isolate units of code for testing without the need for extensive refactoring.
  6. Third-Party Libraries or Frameworks: When dealing with objects from third-party libraries or frameworks, you either cannot modify them or have no control over their internal workings. Mocking the constructors of these objects allows you to simulate their behavior as per your test requirements.
  7. Performance Optimization: When the object’s construction is time-consuming or resource-intensive, mocking the constructor can significantly reduce the overhead, leading to faster and more efficient test execution.

It’s important to note that while mocking constructor calls can be highly beneficial, it should be used judiciously. Overuse of mocking, particularly mocking types you don’t own, can lead to tests that are fragile and overly coupled to implementation details. Always consider if there are simpler alternatives, such as refactoring the code to use dependency injection, which can make the code more testable and maintainable in the long run.

How to Use MockConstruction

To use MockConstruction, you first need to ensure you are using Mockito 3.4.0 or later. For Mockito version 5 or higher, add the following dependency to your pom.xml.


For Mockito versions lower than 5, you must add the mockito-inline dependency:


In this example, let’s consider a scenario where we want to test a PaymentProcessor class that depends on a PaymentService, which is a third-party library. The PaymentService class has a complex constructor that we want to avoid during testing. Our goal is to mock the construction of PaymentService objects to focus on testing the PaymentProcessor functionality.

First, let’s define the PaymentService class provided by the third-party library:

public class PaymentService {
    public PaymentService(String apiKey) {
        // Initialization logic that requires an API key

    public boolean processPayment(double amount) {
        // Logic to process the payment
        return true; // Assume payment is always successful for simplification

Next, we have our PaymentProcessor class that uses PaymentService:

public class PaymentProcessor {
    private String apiKey;

    public PaymentProcessor(String apiKey) {
        this.apiKey = apiKey;

    public boolean makePayment(double amount) {
        PaymentService paymentService = new PaymentService(apiKey);
        return paymentService.processPayment(amount);

We want to test the makePayment method of PaymentProcessor without actually invoking the PaymentService constructor. Here’s how we can achieve this with Mockito’s MockConstruction:

import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PaymentProcessorTest {

    public void testMakePayment() {
        // Mock the construction of PaymentService objects
        try (MockedConstruction<PaymentService> mocked = Mockito.mockConstruction(PaymentService.class, (mock, context) -> {
            // Mock the behavior of processPayment to always return true
            // Create an instance of PaymentProcessor
            PaymentProcessor processor = new PaymentProcessor("dummy-api-key");

            // Call makePayment and verify the result
            boolean result = processor.makePayment(100.0);
            assertTrue(result, "Payment should be successful");

            // Verify that a PaymentService object was constructed
            PaymentService mockService = mocked.constructed().get(0);

In this test, we use Mockito.mockConstruction to intercept the construction of PaymentService objects within the try-with-resources block. We then specify the behavior of the processPayment method to always return true for simplicity. This allows us to test the makePayment method of PaymentProcessor without depending on the actual implementation of PaymentService, focusing solely on the logic within PaymentProcessor.

MockConstruction with Constructor Arguments

When using Mockito’s MockConstruction, handling constructor arguments for the mocked objects can be crucial for tests that depend on the state or behavior influenced by these arguments. Let’s consider a scenario where we’re testing a class that constructs instances of another class with various constructor arguments. We aim to mock the construction of these instances while paying attention to the constructor arguments passed during the test.

Consider a NotificationService class that sends different types of notifications based on the parameters provided to its constructor:

public class NotificationService {
    private final String messageType;
    private final String recipient;

    public NotificationService(String messageType, String recipient) {
        this.messageType = messageType;
        this.recipient = recipient;

    public boolean sendNotification() {
        System.out.println("Sending " + messageType + " to " + recipient);
        // Imagine sending the notification here
        return true;

We have a UserActivityService class that uses the NotificationService to send notifications based on user activity:

public class UserActivityService {
    public void notifyUserActivity(String userId, String activityType) {
        NotificationService notificationService = new NotificationService(activityType, userId);

Our goal is to test UserActivityService without actually sending notifications, which requires mocking NotificationService‘s construction. We want to ensure that NotificationService is constructed with the correct arguments (activityType and userId) in the test.

Here’s how we can achieve this with MockConstruction:

import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;

import static org.mockito.ArgumentMatchers.anyString;

public class UserActivityServiceTest {

    public void testNotifyUserActivity() {
        try (MockedConstruction<NotificationService> mockedConstruction = Mockito.mockConstruction(NotificationService.class, (mock, context) -> {
            // Verify constructor arguments
            String messageType = context.arguments().get(0);
            String recipient = context.arguments().get(1);

            // Assert that the constructor arguments are as expected
            assert "LOGIN".equals(messageType);
            assert "user123".equals(recipient);

            // Mock the behavior of sendNotification to always return true
        })) {
            // Create an instance of UserActivityService
            UserActivityService service = new UserActivityService();

            // Perform the activity notification
            service.notifyUserActivity("user123", "LOGIN");

            // Verify that NotificationService was constructed

In this test, we’re using Mockito.mockConstruction to mock the construction of NotificationService objects. The lambda function provided to mockConstruction gives us access to the MockedConstruction.Context, which allows us to inspect the constructor arguments through context.arguments(). We then assert that these arguments match the expected values for our test scenario. Finally, we mock the sendNotification method to simulate a successful notification send operation without executing real logic.

By handling constructor arguments in MockConstruction, we can write more precise and behaviorally relevant unit tests, ensuring that our classes correctly utilize their dependencies even when those dependencies are mocked.

Benefits of MockConstruction

Mocking object construction offers several benefits that improve the quality and reliability of unit tests:

  • Isolation: By mocking the construction of objects, tests can focus on the unit under test without being affected by external dependencies or complex initialization logic.
  • Simplicity: It simplifies testing classes that interact with objects whose construction is outside the control of the tested class, such as objects created in third-party libraries.
  • Flexibility: Developers gain more control over the test environment, making it easier to test various scenarios, including edge cases and error conditions.
  • Improved Test Coverage: The ability to mock object construction makes areas of the codebase that were previously difficult to test more accessible, leading to higher test coverage.


MockConstruction is a testament to Mockito’s ongoing evolution, offering Java developers an advanced tool for creating more robust and isolated unit tests. By enabling the mocking of object construction, Mockito opens new avenues for testing complex interactions and dependencies, reinforcing the framework’s position as an indispensable asset in the software development toolkit. Whether dealing with legacy code, third-party libraries, or intricate initialization logic, MockConstruction equips developers with the means to write cleaner, more effective tests, contributing to higher-quality software and more reliable codebases.

Anastasios Antoniadis
Follow me
Latest posts by Anastasios Antoniadis (see all)
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x