当传入的rest调用参数为JSON时,Mockito返回null [英] Mockito when returns null when rest call parameter passed in is JSON

查看:74
本文介绍了当传入的rest调用参数为JSON时,Mockito返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码要测试,它采用 JSON 字符串并使用 JSON 字符串中的对象来调用方法

I have a piece of code that I want to test which takes a JSON string and uses the object from the JSON string to call a method

@RequestMapping(value = "/cancel", method = RequestMethod.POST, produces = "application/json")
    public ReservationCancelResponseType cancel(@RequestBody CancelReservationRequest request) {
        ReservationCancelResponseType result = null;

        for(BrandEnum brand : request.getBrands()) {
            switch(brand) {
            case BRAND_NAME:
                result = service.cancel(request);
                break;
            }
        }

        return result;
    }

我正在尝试使用以下代码调用它

I am trying to call this using the following code

@Test
public void testCancel() throws Exception {
    ReservationCancelResponseType responseType = new ReservationCancelResponseType();

    CancelReservationRequest request = new CancelReservationRequest();
    List<BrandEnum> brands = new ArrayList<>();
    brands.add(BrandEnum.BRAND_NAME);
    request.setBrands(brands);

    String requestString = objectMapper.writeValueAsString(request);

    when(service.cancel(request)).thenReturn(responseType);

    this.mockMvc.perform(post("/cancel")
                .contentType(MediaType.APPLICATION_JSON)
                .content(requestString)
            ).andExpect(status().isOk());
}

我认为这不起作用的原因是在 when().thenReturn() 调用中,我传入了一个对象,但在其余调用中我传入了一个 objectMapper 创建的这个对象的 String 版本,所以它们是不同的,所以我为 when().thenReturn() 获取 null代码>调用

I think the reason this is not working is because in the when().thenReturn() call, I am passing in an object but in the rest call I am passing in a String version of this object created by the objectMapper so these are different so I am getting null for the when().thenReturn() call

这是否正确,如果正确,您建议我如何解决此问题?

Is this correct and if so how would you suggest i resolve this?

推荐答案

假设您的控制器在测试流程中使用的服务实例肯定与您在测试中模拟的实例相同,那么最可能的原因是问题是 CancelReservationRequestequals() 实现.当 Mockito 尝试比较您的 when/then 预期的实例时,它要么没有 equals() 要么它的 equals() 实现返回 false使用控制器方法中使用的实例调用.

Assuming that the instance of service which is used by your controller in the test flow definitely the same instance as you are mocking in your test then the likeliest cause of the issue is CancelReservationRequest's implementation of equals(). Either it has no equals() or its equals() implementation is returning false when Mockito attempts to compare the instance expected by your when/then call with the instance used inside your controller method.

您可以通过更改...来验证这一点

You could verify this by changing ...

when(service.cancel(request)).thenReturn(responseType)

...到:

when(service.cancel(Mockito.any(CancelReservationRequest.cla‌ss))).thenReturn(res‌​ponseType)

如果 service.cancel() 方法返回您的响应类型,那么您就会知道问题在于 CancelReservationRequest 的相等性检查.对此的解决方法是实现一个 equals() 方法,该方法允许 Mockito 正确比较您的 when/then 调用预期的实例与控制器方法中使用的实例.如果创建自定义 equals() 方法不是跑步者,您甚至可以使用 Mockito.refEq().

If the service.cancel() method returns your response type then you'll know the issue is with CancelReservationRequest's equality check. The fix for this is to implement an equals() method which allows Mockito to correctly compare the instance expected by your when/then call with the instance used inside your controller method. You could even use Mockito.refEq() if creating a custom equals() method is not a runner.

这篇关于当传入的rest调用参数为JSON时,Mockito返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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