相关疑难解决方法(0)

PowerMockito:使用匹配器模拟静态方法时收到InvalidUseOfMatchersException

当我测试此静态方法时

public class SomeClass {
    public static long someMethod(Map map, String string, Long l, Log log) {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

import org.apache.commons.logging.Log;

@RunWith(PowerMockRunner.class)
//@PrepareForTest(SomeClass.class)
public class Tests {
    @Test
    public void test() {
        ...
        PowerMockito.mockStatic(SomeClass.class);
        Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), isA(Log.class))).thenReturn(1L);
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道了InvalidUseOfMatchersException。我的问题是:

  1. 当所有参数都使用匹配器时,为什么会出现此异常?怎么解决呢?我已经调试了它,发现isA(Log.class)返回null。
  2. 当我将@PrepareForTest注释添加到测试类并运行测试时,junit不会响应。为什么?

编辑

我试图不使用参数匹配器,并得到

org.mockito.exceptions.misusing.MissingMethodInvocationException:when()需要一个参数,该参数必须是“模拟的方法调用”。例如:when(mock.getArticles())。thenReturn(articles);

同样,由于以下原因,可能会出现此错误:

  1. 您存根以下方法之一:final / private / equals()/ hashCode()方法。这些方法不能存根/验证。

  2. 在when()内部,您不会在模拟对象上调用方法,而是在其他对象上调用方法。

在 ...

因此,似乎是由于someMethod本身。该方法中有同步块。我想知道Powermockito是否可以模拟这种方法。

java mockito powermock

5
推荐指数
1
解决办法
8575
查看次数

标签 统计

java ×1

mockito ×1

powermock ×1