Mockito Matchers之间有什么区别是A,any,eq和同样的? [英] What's the difference between Mockito Matchers isA, any, eq, and same?
问题描述
我很困惑他们之间有什么区别,在哪种情况下选择哪一个。一些差异可能是显而易见的,例如任何
和 eq
,但我将它们包括在内只是为了确定。
I am confused on what's the difference between them, and which one to choose in which case. Some difference might be obvious, like any
and eq
, but I'm including them all just to be sure.
我想知道他们之间的差异,因为我遇到了这个问题:
我在Controller类中有这个POST方法
I wonder about their differences because I came across this problem: I have this POST method in a Controller class
public Response doSomething(@ResponseBody Request request) {
return someService.doSomething(request);
}
并且想在该控制器上执行单元测试。
我有两个版本。第一个是简单的,像这样
And would like to perform a unit test on that controller. I have two versions. The first one is the simple one, like this
@Test
public void testDoSomething() {
//initialize ObjectMapper mapper
//initialize Request req and Response res
when(someServiceMock.doSomething(req)).thenReturn(res);
Response actualRes = someController.doSomething(req);
assertThat(actualRes, is(res));
}
但我想使用MockMvc方法,就像这个
But I wanted to use a MockMvc approach, like this one
@Test
public void testDoSomething() {
//initialize ObjectMapper mapper
//initialize Request req and Response res
when(someServiceMock.doSomething(any(Request.class))).thenReturn(res);
mockMvc.perform(post("/do/something")
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(req))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$message", is("done")));
}
两者都运作良好。但是我希望我的 someServiceMock.doSomething()
在MockMvc方法中接收 req
,或者至少是一个拥有的对象与 req
相同的变量值(不只是任何 Request
类),并返回 res
,就像第一个一样。我知道使用MockMvc方法是不可能的(或者是吗?),因为实际调用中传递的对象总是与mock中传递的对象不同。无论如何我能做到吗?或者这样做甚至有意义吗?或者我应该满意使用 any(Request.class)
?我已经尝试 eq
,相同的
,但所有这些都失败了。
Both work well. But I wanted my someServiceMock.doSomething()
in the MockMvc approach to receive req
, or at least an object that has the same variable values as req
(not just any Request
class), and return res
, just like the first. I know that it's impossible using the MockMvc approach (or is it?), because the object passed in the actual call is always different from the object passed in the mock. Is there anyway I can achieve that? Or does it even make sense to do that? Or should I be satisfied using any(Request.class)
? I've tried eq
, same
, but all of them fail.
提前谢谢你。
我希望我能很好地解释自己。
Thank you in advance. I hope I explained myself well.
推荐答案
-
any()
绝对没有检查。在Mockito 1.x中,任何(T.class)
也绝对不会检查任何内容,但也会保存一个演员表(在Java 8之前)。any()
checks absolutely nothing. In Mockito 1.x,any(T.class)
also checks absolutely nothing but also saves you a cast (prior to Java 8).这是由于Mockito 2.0及更高版本的变化,当
任何(T.class)
将共享isA
语义表示任何T
或正确任何类型的实例T
。any()
仍然会检查一切。This is due to change in Mockito 2.0 and beyond, when
any(T.class)
will shareisA
semantics to mean "anyT
" or properly "any instance of typeT
".any()
will still check absolutely nothing.isA(T。 class)
检查参数instanceof T
,暗示它是非空的。isA(T.class)
checks that the argumentinstanceof T
, implying it is non-null.same(obj)
检查参数是否与obj
相同,例如arg == obj
为真。same(obj)
checks that the argument is the same instance asobj
, such thatarg == obj
is true.eq(obj)
根据equals
方法检查参数是否等于obj
。如果您在不使用匹配器的情况下传递实际值,这也是一种行为。eq(obj)
checks that the argument equalsobj
according to itsequals
method. This is also the behavior if you pass in real values without using matchers.请注意,除非
等于
被覆盖,你会看到默认的Object.equals实现,它的行为与相同(obj)
相同。Note that unless
equals
is overridden, you'll see the default Object.equals implementation, which would have the same behavior assame(obj)
.如果您需要更精确的自定义,可以使用适配器作为您自己的谓词:
If you need more exact customization, you can use an adapter for your own predicate:
- 对于Mockito 1.x,使用
argThat
使用自定义HamcrestMatcher< T>
选择完全对象需要。 - 对于Mockito 2.0及更高版本,请使用
Matchers.argThat
并使用自定义org.mockito.ArgumentMatcher< ; T>
,或MockitoHamcrest.arg那个
,带有自定义Hamcrest匹配< T>
。
- For Mockito 1.x, use
argThat
with a custom HamcrestMatcher<T>
that selects exactly the objects you need. - For Mockito 2.0 and beyond, use
Matchers.argThat
with a customorg.mockito.ArgumentMatcher<T>
, orMockitoHamcrest.argThat
with a custom HamcrestMatcher<T>
.
这篇关于Mockito Matchers之间有什么区别是A,any,eq和同样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 对于Mockito 1.x,使用