【问题标题】:Junit Mockito test case for ResponseEntity<?> in spring integration frameworkSpring 集成框架中 ResponseEntity<?> 的 Junit Mockito 测试用例
【发布时间】:2016-12-25 05:52:21
【问题描述】:

我正在尝试模拟外部调用。

 ResponseEntity&lt;?&gt; httpResponse = requestGateway.pushNotification(xtifyRequest);

requestGateway 是一个接口。

public interface RequestGateway
{
ResponseEntity<?> pushNotification(XtifyRequest xtifyRequest);
}

以下是我正在尝试做的测试方法。

 @Test
public void test()
{


    ResponseEntity<?> r=new ResponseEntity<>(HttpStatus.ACCEPTED);

    when(requestGateway.pushNotification(any(XtifyRequest.class))).thenReturn(r);
}

上面的when语句有编译错误,说它是无效类型。即使r是ResponseEntity类型。

谁能帮我解决这个问题?

【问题讨论】:

    标签: junit mockito spring-integration


    【解决方案1】:

    您可以改用类型不安全的方法

    doReturn(r).when(requestGateway.pushNotification(any(XtifyRequest.class)));
    

    或者您可以在模拟时删除类型信息

    ResponseEntity r=new ResponseEntity(HttpStatus.ACCEPTED);
    when(requestGateway.pushNotification(any(XtifyRequest.class))).thenReturn(r);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 2022-10-05
      • 1970-01-01
      相关资源
      最近更新 更多