简体   繁体   English

Mockito 未返回预期的 object 时

[英]Mockito when not returning the expected object

I am trying to return an expected value from a mocked method.我正在尝试从模拟方法返回预期值。

TestClass testClass = TestClass.getInstance();
ClassToMock classToMock = Mockito.mock(ClassToMock.class);
testClass.setClassToMock(classToMock);
ExpectedObject expectedObject = new ExpectedObject("1", "2", "3");
when(classToMock.method(ArgumentMatchers.anyString(), ArgumentMatchers.anyBoolean(),
                        ArgumentMatchers.any(A.class), ArgumentMatchers.any(B.class))
                .thenReturn(expectedObject);
testClass.invokeTestMethod();

The TestClass and invokeTestMethod are like below: TestClass 和 invokeTestMethod 如下所示:

public class TestClass {

  private ClassToMock classToMock;

  public void invokeTestMethod() {
     ExpectedObject expectedObj1 = classToMock.method("A", "B", null, null);
     ::
     ::
     ExpectedObject expectedObj2 = classToMock.method("X", "Y", null, null);
     ::
     ::
     ExpectedObject expectedObj3 = classToMock.method("P", "Q", null, null);
     ::
     ::
  }

  public void setClassToMock(ClassToMock ctm) {
     this.classToMock = ctm;
  }
}

I have set the classToMock instance on the TestClass, to make sure that the TestClass works on the mock instance.我已经在 TestClass 上设置了 classToMock 实例,以确保 TestClass 在模拟实例上工作。

To make the issue clearer, the method call is happening on the mocked object (classToMock), but, the expected return value (ExpectedObject) is not coming.为了使问题更清楚,方法调用发生在模拟的 object (classToMock) 上,但是预期的返回值 (ExpectedObject) 没有出现。

Issue here: null does not match any(X.class) , so mocked value will not be returned此处的问题: nullany(X.class)不匹配,因此不会返回模拟值

If null is passed as argument, then the two last argument matchers in place:如果null作为参数传递,那么最后两个参数匹配器就位:

  • ArgumentMatchers.any(A.class)
  • ArgumentMatchers.any(B.class)

will not be matched .将不匹配

See documentation of any(java.lang.Class) :请参阅any(java.lang.Class)的文档:

Matches any object of given type, excluding nulls .匹配任何给定类型的 object,不包括nulls 。 (marked bold to emphasize) (标记为粗体以强调)

This is because since Mockito 2.1.0 internally try to evaluate the type (class) of the argument.这是因为Mockito 2.1.0在内部尝试评估参数的类型(类)。 And null is an empty reference, not pointing to an instanced object.null是一个空引用,不指向实例化的 object。 No reference to an object, no class of that object can be evaluated:没有参考 object,没有 object 的 class 可以评估:

null instanceOf A // will evaluate to `false`
null instanceOf B // will evaluate to `false`

See Is null check needed before calling instanceof?请参阅在调用 instanceof 之前是否需要检查 null?

Solution: use any() or isNull() to match null解决方法:使用any()isNull()匹配null

To make the mocked methods be called inside, use an argument matcher that will match on null values passed.要在内部调用模拟方法,请使用参数匹配器,该匹配器将匹配传递的null值。

In the first excerpt when method is called with method signature (String, bool, A, B) as parameter, while in the second excerpt is called method with signature (String, String, Object, Object).在第一个摘录中,使用方法签名(String,bool,A,B)作为参数调用方法,而在第二个摘录中,使用签名(String,String,Object,Object)调用方法。 So in the second excerpt classToMock.method("A", "B", null, null) returns null.所以在第二个摘录中 classToMock.method("A", "B", null, null) 返回 null。

you need to pass ClassToMock ro the TestClass for example in the constructor on in a set method.例如,您需要在 set 方法中的构造函数ClassToMock ro 传递给TestClass This way in the test you can pass your mock to test class and in the method invokeTestMethod the mock will be used.这样在测试中你可以通过你的模拟来测试 class 并且在方法invokeTestMethod中将使用模拟。 At the moment your not doing this, so method is called on the real object目前你没有这样做,所以在真正的 object 上调用method

Thanks to all who chose to help.感谢所有选择提供帮助的人。 I found the fix.我找到了解决办法。 In the TestClass, there was a call like this: ExpectedObject expectedObj1 = classToMock.method("A", "B", null, null);在TestClass中有这样一个调用: ExpectedObject expectedObj1 = classToMock.method("A", "B", null, null); The two null parameters caused the issue.两个 null 参数导致了该问题。 So, I overloaded the method inside the ClassToMock with the NON NULL parameters.因此,我使用 NON NULL 参数重载了ClassToMock内的method After this change, the mock is working perfectly fine.进行此更改后,模拟运行良好。

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

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