简体   繁体   English

尝试模拟 rest 模板时在响应中获取 null 指针

[英]Getting null pointer on response when trying to mock rest template

Im trying to mock the below rest call using mockito inside an unit test.我试图在单元测试中使用 mockito 来模拟下面的 rest 调用。

  String url = baseUrl  + "/samples";

  ResponseEntity<SampleDto> response =
                restTemplate.exchange(url, HttpMethod.GET, null, SampleDto.class);

and its mocked using:及其模拟使用:

  when(restTemplate.exchange(eq(anyString()),eq(HttpMethod.GET), null,
            eq(SampleDto.class))).thenReturn(new ResponseEntity<>(new SampleDto(), HttpStatus.OK));

but the response keeps getting null.response不断得到 null。 Any suggestions?有什么建议么?

Please try with the following configuration请尝试以下配置

  when(restTemplate.exchange(Mockito.anyString()
                , Mockito.eq(HttpMethod.GET)
                , null
                , Mockito.<Class<SampleDto>>any())
   ).thenReturn(new ResponseEntity<>(new SampleDto(), HttpStatus.OK));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM