java - 使用 mockito 在单个测试方法中模拟多个 resttemplate
问题描述
resttemplate.getForObject()
我需要为具有不同响应类型的多个调用的方法编写测试用例。所以我这样写:
@Test
public void tempMethodTest(){
doReturn(abc).when(restTemplate).getForObject(anyString(), ArgumentMatchers.<Class<ABC>>any());//1st rest call
doReturn(def).when(restTemplate).getForObject(anyString(), ArgumentMatchers.<Class<DEF>>any());//2nd rest call
doReturn(efg).when(restTemplate).getForObject(anyString(), ArgumentMatchers.<Class<EFG>>any());//3rd rest call
//when(restTemplate.getForObject(anyString(), ArgumentMatchers.<Class<ABC>>any())).thenReturn(abc);
//when(restTemplate.getForObject(anyString(), ArgumentMatchers.<Class<DEF>>any())).thenReturn(def);
//when(restTemplate.getForObject(anyString(), ArgumentMatchers.<Class<EFG>>any())).thenReturn(efg);
assertNotNull(service.tempMethod(obj));
}
但是在测试时,我在第一次 REST 调用期间遇到了提供的异常:
com.example.EFG cannot be cast to com.example.ABC
解决方案
所以最后我找到了解决这个问题的方法。
@Test
public void getBaseStationConfigurationTest(){
when(restTemplate.getForObject(anyString(), any())).thenReturn(abc,def,efg);
assertNotNull(service.tempMethod(obj));
}
推荐阅读
- html - CSS 从左到右滑动面板
- javascript - Href 链接未正确呈现
- javascript - 如何在错误时触发 ajax 请求?
- python-3.x - 熊猫合并有两个结果具有相同的代码和输入数据
- apache - 如何通过 htaccess 重写为双语网站永久附加 X 以完成速记 URL?
- php - 如何在 Lavavel 中授权失败时发送错误消息以查看
- bash - 带有for循环的ansible剧本没有按预期工作
- string - 在Java 8+中将数字的字符串表示形式转换为整数(没有if的单行)
- sentinel - ServiceStack.Redis:没有可用的 Redis 哨兵
- php - 无法将邮件发送到 Gmail dsn=5.0.0,stat=服务不可用