You need to replace SOME_FUNCTION
with a Mockito mock that you can assert invocations on. Depending on your situation, there might be several ways to achieve this:
Pass in the function as a member
If the function you need to perform assertions on does not necessarily need to be final static, and it makes sense in your situation to pass it in:
class MyClass {
final Function<Throwable, Optional<String>> someFunction;
public MyClass(Function<Throwable, Optional<String>> someFunction) {
this.someFunction = someFunction;
}
// ...
}
then you can pass in a mock in your test:
Function<Throwable, Optional<String>> mockFunc = Mockito.mock(Function.class);
var myClass = new MyClass(mockFunc);
// ... test code ...
verify(mockFunc, times(2)).apply(any(Exception.class));
If the function needs to stay static but you still can make the variable non-final, then you can just change it by static assignment and it should work the same. Just remember to set it back to its original value afterwards in this case.
Mock the static member using reflection
If you cannot alter the class' code, and need to perform assertions on the static function, it becomes a bit more tricky. You can attempt to replace the function with a mock using reflection. Mockito has a Whitebox
class which encapsules some of that:
Function<Throwable, Optional<String>> mockFunc = Mockito.mock(Function.class);
Whitebox.setInternalState(MyClass.class, "SOME_FUNCTION", mockFunc);
// ... test code ...
verify(mockFunc, times(2)).apply(any(Exception.class));
But this most probably has some unwanted side effects (like SOME_FUNCTION
staying a mock longer than intended) and should only be a last resort.