Spring 集成框架中 ResponseEntity<?> 的 Junit Mockito 测试用例

标签 junit mockito spring-integration

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

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


requestGateway 是一个接口(interface)。
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 类型。

谁能帮我解决这个问题?

最佳答案

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

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

或者您可以在模拟时删除类型信息
ResponseEntity r=new ResponseEntity(HttpStatus.ACCEPTED);
when(requestGateway.pushNotification(any(XtifyRequest.class))).thenReturn(r);

关于Spring 集成框架中 ResponseEntity<?> 的 Junit Mockito 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39015830/

相关文章:

java - 不存在的枚举值的单元测试?

spring-integration - ExpressionEvaluatingSqlParameterSourceFactory 中静态方法参数的正确语法

java - 使用 Java 的多个 Selenium 参数化 Web 测试

java - Mockito NullPointerException 在 InvocableMatcher 上调用 equals

java - 真正的方法执行而不是模拟

java - 使用 Mockito 测试返回对象的方法

android - 带有 Mockito 或 Easy Mock 的 Robotium

spring-integration - 如何使用 jdbc-outbound-channel-adapter 过滤 Spring Integration 流?

java - 接收: MessageDeliveryException: Dispatcher has no subscribers using inbound-channel-adapter in 2 different spring integration modules

java - IntelliJ 警告 : Unchecked generics array creation for varargs parameter