使用 Jmock 创建模拟以为其自身返回 null 值
我有一个类似于以下内容的方法:
public void myMethod(MyClass c)
{
if (c == null)
{
return;
}
try
{
c.someMethod();
}
catch (SomeException e)
{
// log the exception, possibly re-throw
}
}
我试图找到一种方法来设置 MyClass 参数 c 的模拟实例,使其自身返回 null 值,并且永远不会调用 c.someMethod() 。 我的单元测试如下所示:
@Test
public void Test_myMethod_With_Null_MyClass_Does_Not_Call_someMethod()
{
Mockery mockery = new Mockery()
{{
setImposteriser(ClassImposteriser.INSTANCE);
}};
final MyClass mockMyClass = mockery.mock(MyClass.class);
try
{
mockery.checking(new Expectations()
{{
oneOf(mockMyClass).equals(null);
will(returnValue(true));
never(mockMyClass).someMethod();
}});
}
catch (Exception e)
{
logger.fatal(e);
fail("Exception thrown in test.");
}
Util.myMethod(mockMyClass);
}
基本上,我正在设置 MyClass 的模拟实例,并对其设置期望,即当针对 null 值测试其值时,它将返回 true,并且方法 someMethod() 是从来没有打电话过。
现在,测试失败了,因为 jMock 表示不可能重写 Object 类提供的方法( equals(null) 部分)。
有没有办法用 jMock 做到这一点? 这种模式有意义吗? 这是一个有效的测试吗? 如果没有,有人对如何测试这个有什么建议吗?
I have a method that looks similar to the following:
public void myMethod(MyClass c)
{
if (c == null)
{
return;
}
try
{
c.someMethod();
}
catch (SomeException e)
{
// log the exception, possibly re-throw
}
}
I am trying to find a way to set up a mock instance of the MyClass parameter c such that it returns a value of null for itself, and that c.someMethod() is never called. My unit test looks like this:
@Test
public void Test_myMethod_With_Null_MyClass_Does_Not_Call_someMethod()
{
Mockery mockery = new Mockery()
{{
setImposteriser(ClassImposteriser.INSTANCE);
}};
final MyClass mockMyClass = mockery.mock(MyClass.class);
try
{
mockery.checking(new Expectations()
{{
oneOf(mockMyClass).equals(null);
will(returnValue(true));
never(mockMyClass).someMethod();
}});
}
catch (Exception e)
{
logger.fatal(e);
fail("Exception thrown in test.");
}
Util.myMethod(mockMyClass);
}
Basically, i'm setting up a mock instance of MyClass, and setting the expectations on it that when its value is tested against the null value, it will return true, and that the method someMethod() is never called.
Right now, the test is failing, as jMock says that it's not possible to override methods provided by the Object class (the equals(null) part).
Is there a way to do this with jMock? Does this pattern make sense? Is this a valid test? If not, does anyone have any suggestions on how to test this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不能仅使用 JMock 来测试此代码。
我将通过直接传入
null
来测试这一点。 如果没有抛出或记录任何异常,您就知道您的代码按预期工作。@Test如果您想让它更明确,您可以将
myMethod(null)
调用包装在 try/catch 块中,但是除了在出现故障时澄清故障原因之外,这不是必需的。这是一个足够的测试,因为您知道如果
c
为null
那么c.something()
当然无法调用。I do not think you can test this code using only JMock.
I would test this by passing in
null
directly. If no exceptions are thrown or logged, you know that your code worked as expected.@TestIf you wanted to make it more explicit, you could wrap the
myMethod(null)
call in a try/catch block, but that is not necessary except to clarify the failure reason when there is a failure.This is a sufficient test because you know that if
c
isnull
thenc.something()
of course cannot be called.模拟应该用于测试对象如何与其协作者交互。 如果没有合作者,那就没有什么可嘲笑的。 传入 null 更简单且更具表现力。
Mocks are supposed to be used to test how an object interacts with its collaborators. If there is no collaborator, then there's nothing to mock. Passing in a null is simpler and more expressive.