我有一个非常简单的测试方法:
待测方法:
public static String newUuid() {
return UUID.randomUUID().toString();
}
我用 powermock 来测试它:
@Test
public void testNewUuid() {
mockStatic(UUID.class);
when(UUID.randomUUID().toString()).thenReturn("uuid");
assertEquals("uuid", UuidHelper.newUuid());
}
但是我收到以下错误:
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.
我能知道为什么吗?
我的依赖:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.4</version>
</dependency>