【发布时间】:2016-05-17 08:46:43
【问题描述】:
RestTemplate 和它的测试版有什么区别?当我们通过@ControllerAdvice 进行异常处理时,RestTemplate 会抛出异常,但是对于相同的流测试版本返回包含异常详细信息的 json。
所以,我想看看它们之间的差异摘要。
【问题讨论】:
标签: spring resttemplate spring-test
RestTemplate 和它的测试版有什么区别?当我们通过@ControllerAdvice 进行异常处理时,RestTemplate 会抛出异常,但是对于相同的流测试版本返回包含异常详细信息的 json。
所以,我想看看它们之间的差异摘要。
【问题讨论】:
标签: spring resttemplate spring-test
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
*/
ReflectionTestUtils和ReflectionUtils可以找到类似的模式
【讨论】: