问题
All the examples provided for mockedStatic method is for method without parameters. Is there a way to mock methods with parameters.
examples provided: https://javadoc.io/static/org.mockito/mockito-core/3.4.6/org/mockito/Mockito.html#static_mocks
mocked.when(Foo::method).thenReturn("bar");
assertEquals("bar", Foo.method());
mocked.verify(Foo::method);
}
What I want: I tried below and it does not work.
mocked.when(Foo.methodWithParams("SomeValue"))
回答1:
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")
);
}
来源:https://stackoverflow.com/questions/63172397/mockito-mockedstatic-for-method-with-arguments