【问题标题】:Mocking Rest Exchange for Void API为 Void API 模拟休息交换
【发布时间】:2018-09-01 22:24:00
【问题描述】:

我正在尝试模拟下面的休息电话

restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<User>(user), void.class);

这里是 JUNIT

    Mockito.when(restTemplate.exchange(Matchers.any(), Matchers.eq(HttpMethod.PUT), Matchers.any(HttpEntity.class), Matchers.eq(Void.class))).thenReturn(new ResponseEntity<>(HttpStatus.ACCEPTED));

但问题是当我调试代码时。即使我从模拟中抛出异常,它也总是返回 NULL。

我不确定我在这里做错了什么

【问题讨论】:

    标签: java spring rest junit mockito


    【解决方案1】:

    这是因为Voidvoid.class 的类不同:

    Class<?> c1 = void.class;
    Class<?> c2 = Void.class;
    System.out.println(c1.equals(c2));
    

    打印false

    尝试以下方法:

    Mockito.when(restTemplate.exchange(Matchers.any(),
                                       Matchers.eq(HttpMethod.PUT),
                                       Matchers.any(HttpEntity.class),
                                       Matchers.eq(void.class)))
      .thenReturn(new ResponseEntity<>(HttpStatus.ACCEPTED));
    

    【讨论】:

    • 感谢回复,但这并没有帮助,它只是返回 null
    猜你喜欢
    • 2020-03-08
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 2019-06-10
    • 2021-12-28
    • 2021-08-16
    相关资源
    最近更新 更多