Java 使用mockito在单个测试方法中模拟多个resttemplate
我需要为一个方法编写测试用例,该方法具有多个响应类型不同的Java 使用mockito在单个测试方法中模拟多个resttemplate,java,spring-boot,junit,mockito,Java,Spring Boot,Junit,Mockito,我需要为一个方法编写测试用例,该方法具有多个响应类型不同的restemplate.getForObject()调用。所以我这样写: @Test public void tempMethodTest(){ doReturn(abc).when(restTemplate).getForObject(anyString(), ArgumentMatchers.<Class<ABC>>any());//1st rest call doReturn(def).whe
restemplate.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));
}
所以我终于找到了解决这个问题的办法
@Test
public void getBaseStationConfigurationTest(){
when(restTemplate.getForObject(anyString(), any())).thenReturn(abc,def,efg);
assertNotNull(service.tempMethod(obj));
}
如果是带有两个不同rest模板调用的postForEntity(),该怎么办?
@Test
public void getBaseStationConfigurationTest(){
when(restTemplate.getForObject(anyString(), any())).thenReturn(abc,def,efg);
assertNotNull(service.tempMethod(obj));
}