Spring TestRestTemplate 与 RestTemplate

Spring TestRestTemplate vs RestTemplate

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

找到相似的模式