Spring 试图模拟restClient外部API,但它正在调用java中的实际API
我试图模拟restClient外部API,但它正在调用实际的API,而不是模拟它。 请帮忙,因为我不确定哪里出了问题 我试着嘲笑电话和其他一些事情,但都没用Spring 试图模拟restClient外部API,但它正在调用java中的实际API,spring,mocking,mockito,rest-client,powermockrunner,Spring,Mocking,Mockito,Rest Client,Powermockrunner,我试图模拟restClient外部API,但它正在调用实际的API,而不是模拟它。 请帮忙,因为我不确定哪里出了问题 我试着嘲笑电话和其他一些事情,但都没用 public class TestService { private static final String EXTERNAL_API = "http://LinktoExternalAPI/"; @Autowired RestTemplate restTemplate; public Map<
public class TestService
{
private static final String EXTERNAL_API = "http://LinktoExternalAPI/";
@Autowired
RestTemplate restTemplate;
public Map<String, String> countryCodes()
{
Map<String, String> map = new TreeMap<>();
try
{
ResponseEntity<testCountry[]> responseEntity = restTemplate
.getForEntity(EXTERNAL_API
, testCountry[].class);
List<testCountry> testCountryList = Arrays.asList(responseEntity.getBody());
map = testCountryList.stream()
.collect(Collectors.toMap(testCountry::getCode, testCountry::getName));
}
catch (HttpClientErrorException | HttpServerErrorException httpClientOrServerExc)
{
}
return map;
}
}
这方面的测试用例如下:
@RunWith(PowerMockRunner.class)
public class TestServiceTest
{
@InjectMocks
TestService testService;
@Mock
RestTemplate restTemplate;
private static final String EXTERNAL_API = "http://LinktoExternalAPI/";
@Test
public void testCountryCodes(){
TestCountry testCountry = new TestCountry();
testCountry.setCode("JPN");
testCountry.setName("Japan");
List<testCountry> testCountryList = new ArrayList<testCountry>();
testCountryList.add(testCountry);
Mockito.when(restTemplate.getForEntity(EXTERNAL_API, testCountry[].class)).thenReturn(new ResponseEntity(testCountryList, HttpStatus.OK));
Map<String, String> result = testService.countryCodes();
// result is pulling the actual size of the api instead of mocking and sending me testCountryList size.
<Will mention assertion here>
}
结果是拉取API的实际大小,而不是模拟,并向我发送testCountryList大小。调用实际API的原因可能是您模拟的URL与运行时生成的URL不完全相同,因此找不到模拟并调用实际API。 在这些情况下,可以使用Mockito.any 因此,模拟代码将是Mockito.whenRetemplate.getForEntityMockito.any,Mockito.any。然后返回新的响应EntityTestCountryList,HttpStatus.OK
还可以尝试使用@RunWithMockitoJUnitRunner.class而不是PowerMockRunner.class,因为您似乎不需要PowerMock功能。您模拟了错误的方法定义 具有参数字符串和类的getForObject方法不存在。您需要为方法定义行为 请注意,在本例中,第三个参数varargs未使用,因此它默认为空数组。但是Mockito需要这些信息来模拟正确的调用 Mockito.WhenRetemplate.getForObjectanyString.class、anyClass.class、ArgumentMatchers.any .然后返回结果;
有关更完整的示例,请查看我的答案。这仍然符合实际的api。我只是更改了您提到的内容。我只更改了Mockito.whenRetemplate.getForEntityMockito.any,Mockito.any,然后返回新的响应EntityTestCountryList,HttpStatus.OK;谢谢,但它仍然符合实际的API。我改为MockitoJUnitRunner,并用anyString替换Mockito.any。我在本地尝试了上面的代码,它按预期工作。请尝试按原样使用上述代码,而不是复制itLet us的某些部分。
@RunWith(MockitoJUnitRunner.class)
public class TestServiceTest {
@InjectMocks
private TestService testService;
@Mock
private RestTemplate restTemplate;
@Test
public void testCountryCodes(){
TestCountry testCountry = new TestCountry();
testCountry.setCode("JPN");
testCountry.setName("Japan");
TestCountry[] testCountryList = {
testCountry
};
Mockito.when(restTemplate.getForEntity(Mockito.anyString(), Mockito.any())).thenReturn(new ResponseEntity(testCountryList, HttpStatus.OK));
Map<String, String> result = testService.countryCodes();
// result is pulling the actual size of the API instead of mocking and sending me testCountryList size.
}
}