Mockito嘲笑restTemplate.postForEntity [英] Mockito mocking restTemplate.postForEntity

查看:1863
本文介绍了Mockito嘲笑restTemplate.postForEntity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟restTemplate.postForEntity方法,

实际的方法调用是:

URI myUri = new URI(myString);
HttpEntity<String> myEntity ...


String myResponse = restTemplate.postForEntity(myUri, myEntity, String.class);

我的测试课是:

Mockito.when(restTemplate.postForEntity(any(URI.class), any(HttpEntity.class), eq(String.class))).thenReturn(response);

这行不通;我尝试了其他几种排列方式,也都没有成功.任何建议表示赞赏,谢谢.

This does not work; I have tried several other permutations with no success either. Any suggestions appreciated, thanks.

这是行不通的,我的意思是实际的方法被调用,而不是模拟的方法(因此不会返回所需的结果,等等)

By this does not work I mean that the actual method is called and not the mocked method (so the desired result is not returned etc.)

推荐答案

以下代码对我有用- when(mockRestTemplate.postForEntity(anyString(), any(), eq(String.class))).thenReturn(response);

The following code works for me - when(mockRestTemplate.postForEntity(anyString(), any(), eq(String.class))).thenReturn(response);

这篇关于Mockito嘲笑restTemplate.postForEntity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆