简体   繁体   中英

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.

  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. 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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