'NullPointerException Mock when use RestTemplate

I am new to using mockito and I have a problem. I wrote a controller test and it uses restTemplate to get data from another api, but when I run the test, the variable of this type is null. In result NullPointerException.

What could be the problem?

Controlle:

 @GetMapping(value = "api/cstat", produces = "application/json")
    public ResponseEntity<List<String>> cstat() {
               ResponseEntity<DTO> response =                  // <=====NPE
                            restTemplate.exchange(uri, HttpMethod.GET, entity, DTO.class);    

                    if (response.getBody() != null).     //<==== AND HERE NPE(on future))
                    {
                       ...
                    } else {
                        ...
                    }
}

Test:

   
            Dto expected = new Dto();

            String actual = mockMvc.perform(MockMvcRequestBuilders
                                .get("/api/cstat").with(roleLmsRead()))
                        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source