简体   繁体   English

Mockito.any返回null

[英]Mockito.any returns null

I am trying to mock a static method with parameters like this : 我试图用这样的参数模拟一个静态方法:

Mockito.when(StaticClass.staticMethod(Mockito.any(A.class), 
                                      Mockito.any(B.class), SomeEnum.FOO))
       .thenReturn(true);

I have added the following annotations : 我添加了以下注释:

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Parameterized.class)
@PrepareForTest({StaticClass.class, A.class, B.class})

But Mockito.any always returns null . 但是Mockito.any总是返回null Why ? 为什么?

Firstly, you can't mix matchers with actual arguments. 首先,您不能将匹配器与实际参数混合使用。 You should use a matcher for the SomeEnum argument too: 您也应该为SomeEnum参数使用匹配器:

Mockito.when(StaticClass.staticMethod(Mockito.any(A.class), 
                                      Mockito.any(B.class), Mockito.eq(SomeEnum.FOO))
       .thenReturn(true);

Secondly, the any() methods should return null. 其次,any()方法应该返回null。 That is exactly what they do. 这正是他们所做的。 If you look at the code for these methods they return the default value for the class type if it is primitive wrapper object (like Integer, Boolean etc.) otherwise it returns null: 如果查看这些方法的代码,如果它是原始包装器对象(如Integer,Boolean等),则返回类类型的默认值,否则返回null:

public <T> T returnFor(Class<T> clazz) {
    return Primitives.isPrimitiveOrWrapper(clazz) ? Primitives.defaultValueForPrimitiveOrWrapper(clazz) : null;
}

You are getting things wrong. 你弄错了。 The one and only purpose of matcher methods such as any() is to match the arguments that come in at execution time. 独一无二的匹配方法,例如任何(目的是,以匹配在执行时进来的参数。

You use these methods to instruct the mocking framework what calls you expect to happen. 您可以使用这些方法来指示模拟框架您希望发生的调用。 Or the other way round you use them to say: if this or that is coming in as argument then do this or that. 或者反过来你用它们说:如果这个或那个作为参数出现,那么就这样做。

Therefore you absolutely do not care about the results of matcher invocations. 因此,您绝对不关心匹配器调用的结果。

In that sense your question is indicating that your usage of the mocking framework is going the wrong way. 从这个意义上说,你的问题表明你对模拟框架的使用是错误的。 Thus the only answer we can give regarding your current input: A) do some more research how to use mocking and B) then rework your question to be clear about your problem. 因此,我们可以给出关于您当前输入的唯一答案:A)进行更多研究如何使用模拟和B)然后重新设计您的问题以明确您的问题。

Short answer: Use doReturn().when() instead of when().then() 简答:使用doReturn().when()而不是when().then()

Long answer can be found over here: How do Mockito matchers work? 在这里可以找到很长的答案: Mockito匹配器如何工作?

Matchers return dummy values such as zero, empty collections, or null. 匹配器返回虚拟值,例如零,空集合或null。 Mockito tries to return a safe, appropriate dummy value, like 0 for anyInt() or any(Integer.class) or an empty List for anyListOf(String.class). Mockito尝试返回一个安全的,适当的虚拟值,如0表示anyInt()或任何(Integer.class),或者为anyListOf(String.class)返回一个空列表。 Because of type erasure, though, Mockito lacks type information to return any value but null for any() 但是,由于类型擦除,Mockito缺少类型信息来返回任何值,但null为任何()

NullPointerException or other exceptions: Calls to when(foo.bar(any())).thenReturn(baz) will actually call foo.bar(null), which you might have stubbed to throw an exception when receiving a null argument. NullPointerException或其他异常:调用when(foo.bar(any()))。thenReturn(baz)实际上会调用foo.bar(null),在接收null参数时,您可能已将其存根以引发异常。 Switching to doReturn(baz).when(foo).bar(any()) skips the stubbed behavior. 切换到doReturn(baz).when(foo).bar(any())跳过存根行为。

Side Note: This issue could also be described something like, How to use Mockito matchers on methods that have precondition checks for null parameters? 附注:此问题也可以描述为如何在对空参数进行前置条件检查的方法上使用Mockito匹配器?

It was because it was a Parameterized test, and I did the mockStatic in the @Before method. 这是因为它是参数化测试,我在@Before方法中做了mockStatic。 When I do the mockStatic in the same method, it works. 当我使用相同的方法执行mockStatic时,它可以工作。

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

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