用于ResponseEntity的Junit Mockito测试用例?在Spring集成框架中 [英] Junit Mockito test case for ResponseEntity<?> in spring integration framework

查看:162
本文介绍了用于ResponseEntity的Junit Mockito测试用例?在Spring集成框架中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟外部呼叫.

I am trying to mock the external call.

 ResponseEntity<?> httpResponse = requestGateway.pushNotification(xtifyRequest);

requestGateway是一个接口.

requestGateway is an interface.

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

下面是我要尝试的测试方法.

Below is the test method i am trying to do.

 @Test
public void test()
{


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

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

上面的when语句中出现编译错误,称其为无效类型.即使该类型也是ResponseEntity类型.

A compilation error is there in the above when statement,saying it as an invalid type.even thougg r is of type ResponseEntity.

任何人都可以帮助我解决此问题吗?

Can anyone please help me to solve this issue ?

推荐答案

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

You can instead use the type-unsafe method

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

或者您可以在嘲笑时删除类型信息

Or you can remove the type info while mocking

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

这篇关于用于ResponseEntity的Junit Mockito测试用例?在Spring集成框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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