我在类中有一个静态方法A.我试图用间谍方法来模拟A类。
这些是我写的。
PowerMockito.mockStatic(ConfigManagerProcess.class);
given(ConfigManagerProcess.isApplet()).willReturn(true);
replayAll();
它会抛出以下错误消息:
org.mockito.exceptions.misusing.MissingMethodInvocationException: when()需要一个必须是模拟方法调用的参数。
例如:
when(mock.getArticles()).thenReturn(articles);
此外,此错误可能会显示,因为你存在以下任何一个:
final/private/equals()/hashCode()
方法。那些方法不能 存根/验证。
有没有办法在when语句中调用没有参数的静态方法?
答案 0 :(得分:0)
mocking a static method with no arguments should not be an issue.
The following example works and getInstance does not take an argument:
is
There might be two things that are causing this, without more code to go on I'm going to post both and you'll have to figure out which one works for you.
If mockStatic(ClassA.class);
ClassA mockA = mock(ClassA.class);
when(ClassA.getInstance()).thenReturn(mockA);
is a static method, make sure you have the following code at the top of your test class:
isApplet()
If @PrepareForTest({ConfigManagerProcess.class})
@RunWith(PowerMockRunner.class)
is NOT a static method, you need to make a MOCK OBJECT first.
isApplet()