gpt4 book ai didi

java - 使用mockito在单个测试方法中模拟多个resttemplate

转载 作者:行者123 更新时间:2023-12-01 22:19:27 25 4
gpt4 key购买 nike

我需要为具有不同响应类型的多个 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));
}

关于java - 使用mockito在单个测试方法中模拟多个resttemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58603616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com