我正在使用 jmockit 进行单元测试(使用 TestNG),并且我在使用 Expectations 类来模拟一个使用匹配器将原始类型(布尔值)作为参数的方法时遇到了麻烦。这是一些说明问题的示例代码。
/******************************************************/
import static org.hamcrest.Matchers.is;
import mockit.Expectations;
import org.testng.annotations.Test;
public class PrimitiveMatcherTest {
private MyClass obj;
@Test
public void testPrimitiveMatcher() {
new Expectations(true) {
MyClass c;
{
obj = c;
invokeReturning(c.getFoo(with(is(false))), "bas");
}
};
assert "bas".equals(obj.getFoo(false));
Expectations.assertSatisfied();
}
public static class MyClass {
public String getFoo(boolean arg) {
if (arg) {
return "foo";
} else {
return "bar";
}
}
}
}
/******************************************************/
包含对 invokeReturning(...) 的调用的行会引发 NullPointerException。
如果我将此调用更改为不使用匹配器,如下所示:
invokeReturning(c.getFoo(false), "bas");
它工作得很好。这对我没有好处,因为在我的真实代码中,我实际上是在模拟一个多参数方法,我需要在另一个参数上使用匹配器。在这种情况下,Expectations 类要求所有参数都使用匹配器。
我很确定这是一个错误,或者可能无法将 Matchers 与原始类型一起使用(这会让我感到难过)。有没有人遇到过这个问题,并且知道如何解决它?