Mockito Matchers之间有什么区别是A,any,eq和同样的? [英] What's the difference between Mockito Matchers isA, any, eq, and same?

查看:2063
本文介绍了Mockito Matchers之间有什么区别是A,any,eq和同样的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑他们之间有什么区别,在哪种情况下选择哪一个。一些差异可能是显而易见的,例如任何 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 share isA semantics to mean "any T" or properly "any instance of type T". any() will still check absolutely nothing.

      isA(T。 class)检查参数 instanceof T ,暗示它是非空的。

      isA(T.class) checks that the argument instanceof T, implying it is non-null.

      same(obj)检查参数是否与 obj 相同,例如 arg == obj 为真。

      same(obj) checks that the argument is the same instance as obj, such that arg == obj is true.

      eq(obj)根据 equals 方法检查参数是否等于 obj 。如果您在不使用匹配器的情况下传递实际值,这也是一种行为。

      eq(obj) checks that the argument equals obj according to its equals 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 as same(obj).

      如果您需要更精确的自定义,可以使用适配器作为您自己的谓词:

      If you need more exact customization, you can use an adapter for your own predicate:


      • 对于Mockito 1.x,使用 argThat 使用自定义Hamcrest Matcher< T> 选择完全对象需要。

      • 对于Mockito 2.0及更高版本,请使用 Matchers.argThat 并使用自定义 org.mockito.ArgumentMatcher< ; T> ,或 MockitoHamcrest.arg那个,带有自定义Hamcrest 匹配< T>

      • For Mockito 1.x, use argThat with a custom Hamcrest Matcher<T> that selects exactly the objects you need.
      • For Mockito 2.0 and beyond, use Matchers.argThat with a custom org.mockito.ArgumentMatcher<T>, or MockitoHamcrest.argThat with a custom Hamcrest Matcher<T>.

      这篇关于Mockito Matchers之间有什么区别是A,any,eq和同样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆