英文:
Mockito thenReturn not working, returns Null
问题
我正试图模拟RestTemplate.exchange
方法。主要的类如下所示:
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;
}
}
帖子类是:
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 + "]";
}
}
为了测试上述类,我有以下代码:
@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);
}
}
输出是:
null
Post [userId=null, id=null, title=null, body=null]
为什么when().thenReturn()
与RestTemplate.exchange()
不起作用?我尝试了类似的方法RestTemplate.getForEntity()
,它可以正常工作。
英文:
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.
答案1
得分: 0
以下是您要翻译的内容:
"RequestEntity不是您用于模拟的同一个实例,因为它是在fetchPostEntity函数内创建的。您可以尝试:
when(mockedRestTemplate.exchange(eq(post1Uri), eq(HttpMethod.GET), any(HttpEntity.class), any())).thenReturn(fakeRespEntity);
有关更多关于参数匹配器的信息,请参阅https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/ArgumentMatchers.html。"
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论