Mockito.mockedStatic for method with arguments

Edit – Mockito 3.7.7

Mockito 3.7.7 unified order of verify parameters (Issue #2173)

Updated code:

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

Original answer

It is possible, you need to use a lambda instead of a method reference:

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)