【发布时间】:2018-06-10 20:44:21
【问题描述】:
我有一个 dao 实现的测试用例。
测试类代码 -
@RunWith(SpringRunner.class)
@RestClientTest({XyzDaoImpl.class})
@TestPropertySource(locations = "classpath:application-test.properties")
public class XyzDaoTest {
@Autowired
XyzDaoImpl xyzDaoImpl;
@Test
public void testGetXyzDetails(){
assertThat(xyzDaoImpl.getXyzDetails("123", null)).isNotNull();
}
}
xyzDaoImpl.getXyzDetails 方法实现使用 RestTemplate 调用后端。
当我的配置类代码包含 -
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
但如果我使用构建器创建其余模板来设置超时等,测试失败 -
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.setConnectTimeout(timeout)
.build();
}
尝试使用 @Profile("test") 默认和 @Profile("!test") - 自定义设置创建 Bean,但测试仍然失败。
错误是 -
testException = java.lang.AssertionError: No further requests expected: HTTP POST http://... url.
【问题讨论】:
标签: spring testing spring-boot junit