gpt4 book ai didi

spring - 如何为 Resttemplate 交换方法编写 mockito junit

转载 作者:IT老高 更新时间:2023-10-28 13:56:37 24 4
gpt4 key购买 nike

如何为以下方法编写 mockito junit:

@Autowired
RestTemplate restTemplate;

ResponseEntity<?> execute(final String url, HttpMethod httpMethod,
HttpEntity<?> entityRequest,
String.class,
Map<String, String> urlVariables){
restTemplate.exchange(url, httpMethod, entityRequest, responseType, urlVariables);
}

请教我怎么写。

最佳答案

@RunWith(MockitoJUnitRunner.class)
public class ToTestTest {

@InjectMocks
private YourClass toTest;

@Mock
private RestTemplate template;

@Test
public void test() {
toTest.execute(Mockito.anyString(), Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any());

Mockito.verify(template, Mockito.times(1))
.exchange(Mockito.anyString(),
Mockito.<HttpMethod> any(),
Mockito.<HttpEntity<?>> any(),
Mockito.<Class<?>> any(),
Mockito.<String, String> anyMap());
}
}

关于spring - 如何为 Resttemplate 交换方法编写 mockito junit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30838512/

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