简体   繁体   English

使用Jmock创建一个mock来为自己返回一个null值

[英]Creating a mock with Jmock to return a null value for itself

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. 我试图找到一种方法来设置MyClass参数c的模拟实例,使其为自身返回null值,并且永远不会调用c.someMethod()。 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. 基本上,我正在设置一个MyClass的模拟实例,并设置它的期望值,当它的值被测试为空值时,它将返回true,并且方法someMethod()永远不会被调用。

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). 现在,测试失败了,因为jMock说不可能覆盖Object类提供的方法(equals(null)部分)。

Is there a way to do this with jMock? 有没有办法用jMock做到这一点? Does this pattern make sense? 这种模式有意义吗? Is this a valid test? 这是一个有效的测试吗? If not, does anyone have any suggestions on how to test this? 如果没有,有人对如何测试有任何建议吗?

I do not think you can test this code using only JMock. 我不认为你只能使用JMock来测试这段代码。

I would test this by passing in null directly. 我会通过直接传入null来测试这个。 If no exceptions are thrown or logged, you know that your code worked as expected.@Test 如果没有抛出或记录异常,您就知道您的代码按预期工作了。@ 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. 如果你想让它更明确,你可以将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() ); 
    }
}

This is a sufficient test because you know that if c is null then c.something() of course cannot be called. 这是一个充分的测试,因为你知道如果cnull那么c.something()当然不能被调用。

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. 传入null更简单,更具表现力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM