Kri*_*den 5 java unit-testing jmockit mocking
我正在使用 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";
}
}
}
}
/******************************************************/
Run Code Online (Sandbox Code Playgroud)
包含调用 invokeReturning(...) 的行抛出 NullPointerException。
如果我将此调用更改为不使用匹配器,如下所示:
invokeReturning(c.getFoo(false), "bas");
Run Code Online (Sandbox Code Playgroud)
它工作得很好。这对我没有好处,因为在我的真实代码中,我实际上是在模拟一个多参数方法,我需要在另一个参数上使用匹配器。在这种情况下,Expectations 类要求所有参数都使用匹配器。
我很确定这是一个错误,或者可能无法将 Matchers 与原始类型一起使用(这会让我感到难过)。有没有人遇到过这个问题,并知道如何解决它?
所以问题似乎出在 Expectations.with() 中:
protected final <T> T with(Matcher<T> argumentMatcher)
{
argMatchers.add(argumentMatcher);
TypeVariable<?> typeVariable = argumentMatcher.getClass().getTypeParameters()[0];
return (T) Utilities.defaultValueForType(typeVariable.getClass());
}
Run Code Online (Sandbox Code Playgroud)
对 typeVariable.getClass() 的调用没有达到作者预期的效果,对 Utilities.defaultValueFor 类型的调用返回 null。原始布尔值的去自动装箱是 NPE 的来源。
我通过将 invokeReturning(...) 调用更改为:
invokeReturning(withEqual(false)), "bas");
Run Code Online (Sandbox Code Playgroud)
我不再在这里使用匹配器,但它足以满足我的需要。