使用 Jmock 创建模拟以为其自身返回 null 值

发布于 2024-07-19 02:42:13 字数 1206 浏览 7 评论 0原文

我有一个类似于以下内容的方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

著墨染雨君画夕 2024-07-26 02:42:13

我认为您不能仅使用 JMock 来测试此代码。

我将通过直接传入 null 来测试这一点。 如果没有抛出或记录任何异常,您就知道您的代码按预期工作。@Test

public void Test_myMethod_With_Null_MyClass_Does_Not_Call_someMethod()
{
    Util.myMethod(null);
    //no exception should be thrown.
}

如果您想让它更明确,您可以将 myMethod(null) 调用包装在 try/catch 块中,但是除了在出现故障时澄清故障原因之外,这不是必需的。

public void Test_myMethod_With_Null_MyClass_Does_Not_Call_someMethod()
{
    try
    {
        Util.myMethod(null);
    }
    catch( ExpectedException e )
    {
        fail( "Should not have thrown exception: " + e.getMessage() ); 
    }
}

这是一个足够的测试,因为您知道如果 cnull 那么 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.@Test

public void Test_myMethod_With_Null_MyClass_Does_Not_Call_someMethod()
{
    Util.myMethod(null);
    //no exception should be thrown.
}

If 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.

public void Test_myMethod_With_Null_MyClass_Does_Not_Call_someMethod()
{
    try
    {
        Util.myMethod(null);
    }
    catch( ExpectedException e )
    {
        fail( "Should not have thrown exception: " + e.getMessage() ); 
    }
}

This is a sufficient test because you know that if c is null then c.something() of course cannot be called.

森林散布 2024-07-26 02:42:13

模拟应该用于测试对象如何与其协作者交互。 如果没有合作者,那就没有什么可嘲笑的。 传入 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文