简体   繁体   中英

Mockito thenReturn not working, returns Null

I am trying to mock the RestTemplate.exchange method. The main class is as follows:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class JsonPlaceHolder {
    
    private RestTemplate restTemplate;

    public JsonPlaceHolder(RestTemplate restTemplate) {
        super();
        this.restTemplate = restTemplate;
    }

    
    public Post fetchPostEntity() {
        HttpHeaders headers = new HttpHeaders();
        Post reqPost = new Post();
        HttpEntity<Post> requestEntity = new HttpEntity<Post>(reqPost, headers);
        ResponseEntity<Post> respEntity = restTemplate.exchange("https://jsonplaceholder.typicode.com/posts/1",
                HttpMethod.GET, requestEntity, Post.class);
        System.out.println(respEntity);     
        return reqPost;     
    }
}

The post class is:

public class Post {
    private String userId;
    private String id;
    private String title;
    private String body;
    
    // Getters and Setters

    @Override
    public String toString() {
        return "Post [userId=" + userId + ", id=" + id + ", title=" + title + ", body=" + body + "]";
    }
    
}

To test the above class I have the following code

@RunWith(MockitoJUnitRunner.class)
public class JsonPlaceHolderTest {
    
    @Mock
    private RestTemplate mockedRestTemplate;
    private String post1Uri = "https://jsonplaceholder.typicode.com/posts/1";

    
    
    @Test
    public void restTemplateExchange() {
        JsonPlaceHolder jsonPlaceHolder = new JsonPlaceHolder(mockedRestTemplate);
        
        Post fakePost = new Post();
        fakePost.setBody("BODY");
        fakePost.setId("44");
        fakePost.setUserId("USERID");
        fakePost.setTitle("TITLE");
        
        HttpHeaders headers = new HttpHeaders();
        
        Post reqPost = new Post();
        HttpEntity<Post> requestEntity = new HttpEntity<Post>(reqPost, headers);
        
        
        ResponseEntity<Post> fakeRespEntity = new ResponseEntity<Post>(fakePost, HttpStatus.OK);
        
        
        when(mockedRestTemplate.exchange(post1Uri, HttpMethod.GET, requestEntity, Post.class)).thenReturn(fakeRespEntity);
        
        Post respPost = jsonPlaceHolder.fetchPostEntity();
        
        System.out.println(respPost);
    }

}

The output is:

null
Post [userId=null, id=null, title=null, body=null]

Why is when().thenReturn() not working with RestTemplate.exchange() . I tried the similar thing with RestTemplate.getForEntity() and that works.

The RequestEntity is not the same instance you use for your mock as it is being created inside the fetchPostEntity function. You could try:

when(mockedRestTemplate.exchange(eq(post1Uri), eq(HttpMethod.GET), any(HttpEntity.class), any()).thenReturn(fakeRespEntity);

See https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/ArgumentMatchers.html for more information on argument matchers.

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