测试 UUID 抛出 org.mockito.exceptions.misusing.MissingMethodInvocationException

问题描述

我有一个非常简单的测试方法

要测试的方法

  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>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)