gpt4 book ai didi

Spring TestRestTemplate 与 RestTemplate

转载 作者:行者123 更新时间:2023-12-02 02:06:17 25 4
gpt4 key购买 nike

RestTemplate 与其测试版本有什么区别?当我们通过@ControllerAdvice进行异常处理时,RestTemplate会抛出异常,但对于相同的流程测试版本会返回包含异常详细信息的json。

所以,我想看看它们之间的差异摘要。

最佳答案

restTemplate给你更多的可能性,testRestTemplate只是restTemplate的包装,它为你提供了方便的方法,就像你说的,它不不抛出异常,而是用json响应包装它,这样的行为应该在实际应用中由您自己实现,但您在测试中可能不关心。

这是来自testRestTemplate的javadoc

/**
* Convenient subclass of {@link RestTemplate} that is suitable for integration tests.
* They are fault tolerant, and optionally can carry Basic authentication headers. If
* Apache Http Client 4.3.2 or better is available (recommended) it will be used as the
* client, and by default configured to ignore cookies and redirects.
*
* @author Dave Syer
* @author Phillip Webb
*/

通过ReflectionTestUtilsReflectionUtils可以找到类似的模式

关于Spring TestRestTemplate 与 RestTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37271272/

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