Mockito.mockStatic()

Mock static methods without PowerMock

Now with Mockito 3, you can mock Classes with static methods.

Here is an example:

try (MockedStatic<Foo> dummyStatic = Mockito.mockStatic(ClassName.class)) {
    dummyStatic.when(() -> ClassName.method("param1"))
               .thenReturn("someValue");
    // when
    System.out.println(ClassName.method("param1"));
    //then
    dummyStatic.verify(
            () -> ClassName.method("param1"),
            times(1), 
    );
}

Last updated