1
我有類似如下:懲戒靜態類中的Mockito
public class Foo {
public static String getValue(String key) {
return key + "_" + System.currentTimeMillis();
}
}
現在我需要有一個測試用例,並在這裏是我嘗試:
@Test
public void testFoo() {
PowerMockito.mockStatic(Foo.class);
PowerMockito.when(Foo.getValue("123")).thenReturn("abcd");
PowerMockito.verifyStatic();
}
當我運行測試用例,我得到這個:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:495)
我做錯了什麼?
請指教。
你說得對。我添加了PrepareForTest註釋,它工作。 – serverfaces