Mockito的thenReturn不起作用,返回Null。

huangapple go评论78阅读模式
英文:

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&lt;Post&gt; requestEntity = new HttpEntity&lt;Post&gt;(reqPost, headers);
		ResponseEntity&lt;Post&gt; respEntity = restTemplate.exchange(&quot;https://jsonplaceholder.typicode.com/posts/1&quot;,
				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 &quot;Post [userId=&quot; + userId + &quot;, id=&quot; + id + &quot;, title=&quot; + title + &quot;, body=&quot; + body + &quot;]&quot;;
	}
	
}

To test the above class I have the following code

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

	
	
	@Test
	public void restTemplateExchange() {
		JsonPlaceHolder jsonPlaceHolder = new JsonPlaceHolder(mockedRestTemplate);
		
		Post fakePost = new Post();
		fakePost.setBody(&quot;BODY&quot;);
		fakePost.setId(&quot;44&quot;);
		fakePost.setUserId(&quot;USERID&quot;);
		fakePost.setTitle(&quot;TITLE&quot;);
		
		HttpHeaders headers = new HttpHeaders();
		
		Post reqPost = new Post();
		HttpEntity&lt;Post&gt; requestEntity = new HttpEntity&lt;Post&gt;(reqPost, headers);
		
		
		ResponseEntity&lt;Post&gt; fakeRespEntity = new ResponseEntity&lt;Post&gt;(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.

huangapple
  • 本文由 发表于 2020年8月2日 03:01:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63209015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定