简体   繁体   English

在 mockito 测试中获取剩余模板交换的 null 响应

[英]Getting null response of resttemplate exchange in a mockito test

i am trying to write the mock test case for RestServiceImpl class.我正在尝试为 RestServiceImpl class 编写模拟测试用例。 Below is the code.下面是代码。 And i have a test class shown below RestServiceImplTest.我有一个测试 class,如下所示 RestServiceImplTest。 When i run the test class it returning null from line restTemplate.exchange(UrlString, HttpMethod.POST, request, Object.class)当我运行测试 class 它从行 restTemplate.exchange(UrlString, HttpMethod.POST, request, Object.class) 返回 null

    public class RestServiceImpl 
   private RestTemplate restTemplate;

   @Autowired
   public RestServiceImpl(RestTemplate restTemplate) {
      this.restTemplate = restTemplate;
   }

   @Override
   public ResponseEntity<Object> restService(DataRequest dataRequest) throws Exception {
      ResponseEntity<Object> response = null;
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_JSON);

      HttpEntity<DataRequest> request = new HttpEntity<>(dataRequest, headers);
      try {
         response = restTemplate.exchange(UrlString, HttpMethod.POST, request, Object.class);
      } catch (Exception ex) {
         throw ex;
      }
      return response;
   }
}

Test class测试 class

@RunWith(SpringRunner.class)
public class RestServiceImplTest {

   private RestServiceImpl restServiceImpl;

   @Mock
   private RestTemplate restTemplate;
   @Mock
   private DataRequest dataRequest;
   @Before
   public void setUp() {
      restServiceImpl = new RestServiceImpl(restTemplate);
      dataRequest = new DataRequest();
   }

   @Test
   public void testRestServiceImplwithSuccess() throws Exception {
      ResponseEntity<Object> resp = new ResponseEntity<Object>(HttpStatus.OK);
      doReturn(resp).when(restTemplate).exchange(any(URI.class), any(HttpMethod.class), any(HttpEntity.class),
            any(Class.class));
      ResponseEntity<Object> mockResp = restServiceImpl.restService(DataRequest);
   }

Can anybody tell me where is it going wrong.谁能告诉我哪里出错了。

Almost certainly this is happening because your doReturn is not being used due to the parameters not being met (your any() s).几乎可以肯定这种情况正在发生,因为由于未满足参数(您的any() s)而未使用您的doReturn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于 Resttemplate 交换方法的 Mockito - Mockito for Resttemplate exchange method Spring 引导 / Mockito:Mocking RestTemplate 但始终响应 Null - Spring Boot / Mockito: Mocking RestTemplate but Response Always Null 应用Mockito时,返回restTemplate.exchange(url,HttpMethod.POST,entity1,Response.class))空对象,而不是响应实体存根数据 - restTemplate.exchange(url,HttpMethod.POST,entity1,Response.class)) is returned null object instead of responseEntity stubbed data when mockito applied 获取 null 值作为 restTemplate 调用中的响应 - Getting null values as response in restTemplate call 使用泛型与RestTemplate.exchange的Java Mockito - Java Mockito with RestTemplate.exchange using Generics Mocking RestTemplate 交换返回 null - Mocking RestTemplate exchange returns null 从邮递员 Spring Mvc 获取时未从 resttemplate.exchange 获取响应正文 - Not getting Response Body from resttemplate.exchange while getting from postman Spring Mvc 用于 resttemplate 和 retryTemplate 的 JAVA mockito 单元测试 - JAVA mockito unit test for resttemplate and retryTemplate 从使用Mockito模拟的restTemplate.exchange()中检索httpEntity对象 - Retrieve the httpEntity object from restTemplate.exchange() mocked using Mockito 无法模拟 Spring `RestTemplate` 交换方法与 Mockito - Can't mock Spring `RestTemplate` exchange method with Mockito
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM