Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use Mockito to mock private methods in a test-driven development (TDD) approach


Can I use Mockito to mock private methods in a test-driven development (TDD) approach


Yes, you can use Mockito to mock private methods in a test-driven development (TDD) approach, but with some limitations.

Mockito's primary approach to mocking private methods is through the use of the `spy()` method. This allows you to create a partial mock of an object, where you can override the behavior of both public and private methods.

Here's an example of how you can use Mockito to mock a private method in a TDD approach:

java
public class MyClass {
    private int privateMethod(int x) {
        // Private method implementation
        return x * 2;
    }

    public int publicMethod(int y) {
        // Calling the private method
        return privateMethod(y);
    }
}

@Test
public void testPublicMethod() {
    // Create a spy on the MyClass instance
    MyClass myClassSpy = Mockito.spy(new MyClass());

    // Mock the private method
    Mockito.when(myClassSpy.privateMethod(Mockito.anyInt())).thenReturn(100);

    // Call the public method and assert the expected behavior
    int result = myClassSpy.publicMethod(5);
    Assertions.assertEquals(100, result);

    // Verify that the private method was called
    Mockito.verify(myClassSpy, Mockito.times(1)).privateMethod(Mockito.anyInt());
}

In this example, we first create a spy on the `MyClass` instance using `Mockito.spy(new MyClass())`. This allows us to override the behavior of the private `privateMethod()` method.

We then use `Mockito.when(myClassSpy.privateMethod(Mockito.anyInt())).thenReturn(100)` to mock the private method and make it return a fixed value of 100.

Finally, we call the public `publicMethod()` and assert that the result is 100, as expected. We also verify that the private `privateMethod()` was called once.

It's important to note that while Mockito can be used to mock private methods through the use of spies, this approach has some limitations:

1. Accessibility: Private methods are not accessible from outside the class, so Mockito has to use reflection or other techniques to interact with them, which can make the tests more fragile.
2. Testability: Mocking private methods can be a sign that the class design could be improved, as it may indicate that the class has too many responsibilities or is not sufficiently testable. It's generally better to refactor the code to make it more testable, rather than relying on mocking private methods.

In general, it's recommended to focus on testing the public API of your classes and avoid mocking private methods whenever possible. If you find yourself needing to mock private methods frequently, it may be a sign that your class design could be improved, and you should consider refactoring your code to make it more testable.

Citations:
[1] https://intellipaat.com/blog/tutorial/mockito-tutorial/methods-of-mockito/
[2] https://www.javatpoint.com/methods-of-mockito
[3] https://en.wikipedia.org/wiki/Mockito
[4] https://ioflood.com/blog/mockito-verify/
[5] https://www.vogella.com/tutorials/Mockito/article.html