Mock static methods with Mockito – Java Unit Testing

One of the most frustrating element when I am doing the Unit tests in Java is to not be able to mock static methods.

Sure, during a long time we got PowerMock. But, since JUnit5, PowerMock wasn’t compatible and we were unable to continue with it.


This content originally appeared on DEV Community and was authored by Maxime Guilbert

One of the most frustrating element when I am doing the Unit tests in Java is to not be able to mock static methods.

Sure, during a long time we got PowerMock. But, since JUnit5, PowerMock wasn't compatible and we were unable to continue with it.

But now, we have this feature included in Mockito!

Dependencies

First, you have to add the mockito-inline library. (You can use any version up to 3.4.0)

Here is an exemple with Maven

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <version>3.9.0</version>
    <scope>test</scope>
</dependency>

Writing test

To mock a static method, you have to create a mockStatic of the class with the static method, and declare the event you want to apply to your mock.

example

@Test
public void test() {

try (MockedStatic<LoggerFactory> loggerFactoryMock = Mockito.mockStatic(LoggerFactory.class)) {
            loggerFactoryMock.when(() -> LoggerFactory.getLogger(any(Class.class))).thenReturn(loggerMock);

...
            verify(loggerMock, times(1)).error("Err message test");
...
        }
    }

Then, you just have to run your tests!

I hope it will help you!


This content originally appeared on DEV Community and was authored by Maxime Guilbert


Print Share Comment Cite Upload Translate Updates
APA

Maxime Guilbert | Sciencx (2021-05-11T20:46:52+00:00) Mock static methods with Mockito – Java Unit Testing. Retrieved from https://www.scien.cx/2021/05/11/mock-static-methods-with-mockito-java-unit-testing/

MLA
" » Mock static methods with Mockito – Java Unit Testing." Maxime Guilbert | Sciencx - Tuesday May 11, 2021, https://www.scien.cx/2021/05/11/mock-static-methods-with-mockito-java-unit-testing/
HARVARD
Maxime Guilbert | Sciencx Tuesday May 11, 2021 » Mock static methods with Mockito – Java Unit Testing., viewed ,<https://www.scien.cx/2021/05/11/mock-static-methods-with-mockito-java-unit-testing/>
VANCOUVER
Maxime Guilbert | Sciencx - » Mock static methods with Mockito – Java Unit Testing. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/11/mock-static-methods-with-mockito-java-unit-testing/
CHICAGO
" » Mock static methods with Mockito – Java Unit Testing." Maxime Guilbert | Sciencx - Accessed . https://www.scien.cx/2021/05/11/mock-static-methods-with-mockito-java-unit-testing/
IEEE
" » Mock static methods with Mockito – Java Unit Testing." Maxime Guilbert | Sciencx [Online]. Available: https://www.scien.cx/2021/05/11/mock-static-methods-with-mockito-java-unit-testing/. [Accessed: ]
rf:citation
» Mock static methods with Mockito – Java Unit Testing | Maxime Guilbert | Sciencx | https://www.scien.cx/2021/05/11/mock-static-methods-with-mockito-java-unit-testing/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.