首页 > 解决方案 > 模拟新对象

问题描述

我正在尝试使用以下代码段传递我的 responseEntityMock,但它不起作用。

ResponseEntity<byte[]> response =
          new RestTemplate().exchange(url, HttpMethod.POST, requestEntity, byte[].class);

谁能告诉我如何为这段代码传递我自己的 ResponseEntityMock 。谢谢。

我正在使用以下代码:

   ResponseEntity<byte[]> responseEntity = PowerMockito.mock(ResponseEntity.class);
    RestTemplate restTemplateMock = PowerMockito.mock(RestTemplate.class);
    PowerMockito.whenNew(RestTemplate.class).withAnyArguments().thenReturn(restTemplateMock);
    when(restTemplateMock.exchange(eq(path), eq(POST), any(),
            eq(byte[].class))).thenReturn(responseEntity);

标签: javajunitmockitopowermockito

解决方案


推荐阅读