For the complete documentation index, see llms.txt. This page is also available as Markdown.

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