Mockito thenReturn 不工作,returns Null
Mockito thenReturn not working, returns Null
我正在尝试模拟 RestTemplate.exchange
方法。主要class如下:
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;
}
}
postclass是:
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 + "]";
}
}
为了测试上面的内容class我有下面的代码
@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()
尝试了类似的事情,并且有效。
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。
我正在尝试模拟 RestTemplate.exchange
方法。主要class如下:
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;
}
}
postclass是:
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 + "]";
}
}
为了测试上面的内容class我有下面的代码
@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()
尝试了类似的事情,并且有效。
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。