编辑:这个问题特别与spring-boot 1.4.0中引入的@RestClientTest注释有关,该注释旨在替换工厂方法。
问题:
根据documentation,@RestClientTest应该正确配置MockRestServiceServer,以便在测试REST客户端时使用。但是,在运行测试时,我收到一个IllegalStateException,显示MockServerRestTemplateCustomizer尚未绑定到RestTemplate。
值得注意的是,我使用Gson进行反序列化,而不是Jackson,因此排除。
有人知道如何正确使用这个新的注解吗?我没有发现任何需要更多配置的示例,而我已经这样做了。
配置:
@SpringBootConfiguration
@ComponentScan
@EnableAutoConfiguration(exclude = {JacksonAutoConfiguration.class})
public class ClientConfiguration {
...
@Bean
public RestTemplateBuilder restTemplateBuilder() {
return new RestTemplateBuilder()
.rootUri(rootUri)
.basicAuthorization(username, password);
}
}
客户端:
@Service
public class ComponentsClientImpl implements ComponentsClient {
private RestTemplate restTemplate;
@Autowired
public ComponentsClientImpl(RestTemplateBuilder builder) {
this.restTemplate = builder.build();
}
public ResponseDTO getComponentDetails(RequestDTO requestDTO) {
HttpEntity<RequestDTO> entity = new HttpEntity<>(requestDTO);
ResponseEntity<ResponseDTO> response =
restTemplate.postForEntity("/api", entity, ResponseDTO.class);
return response.getBody();
}
}
测试
@RunWith(SpringRunner.class)
@RestClientTest(ComponentsClientImpl.class)
public class ComponentsClientTest {
@Autowired
private ComponentsClient client;
@Autowired
private MockRestServiceServer server;
@Test
public void getComponentDetailsWhenResultIsSuccessShouldReturnComponentDetails() throws Exception {
server.expect(requestTo("/api"))
.andRespond(withSuccess(getResponseJson(), APPLICATION_JSON));
ResponseDTO response = client.getComponentDetails(requestDto);
ResponseDTO expected = responseFromJson(getResponseJson());
assertThat(response, is(expectedResponse));
}
}
但有一个例外:
java.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since MockServerRestTemplateCustomizer has not been bound to a RestTemplate
答案:
根据下面的答案,不需要在上下文中声明RestTemplateBuilder bean,因为spring-boot自动配置已经提供了它。
如果项目是spring-boot应用程序(它有@SpringBootApplication注释),这将按预期工作。然而,在上面的例子中,项目是一个客户端库,因此没有主应用程序。
为了确保在主应用程序上下文中正确注入RestTemplateBuilder ( bean已被移除),组件扫描需要一个自定义过滤器(@SpringBootApplication使用的过滤器)
@ComponentScan(excludeFilters = {
@ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)
})
发布于 2016-08-22 12:22:58
你在两个地方有RestTemplateBuilder。在ClientConfiguration类和ComponentsClientImpl类。Spring boot 1.4.0自动配置可用于在需要时创建RestTemplate实例的RestTemplateBuilder。从ClientConfiguration类中删除以下代码并运行测试。
@Bean
public RestTemplateBuilder restTemplateBuilder() {
return new RestTemplateBuilder()
.rootUri(rootUri)
.basicAuthorization(username, password);
}
发布于 2016-08-22 12:35:45
应该使用RestTemplate
从静态工厂构造MockRestServiceServer
实例。有关测试过程的详细描述,请参阅this文章。
在您的示例中,您可以执行以下操作:
@RunWith(SpringRunner.class)
@RestClientTest(ComponentsClientImpl.class)
public class ComponentsClientTest {
@Autowired
private ComponentsClient client;
@Autowired
private RestTemplate template;
private MockRestServiceServer server;
@Before
public void setUp() {
server= MockRestServiceServer.createServer(restTemplate);
}
/*Do your test*/
}
https://stackoverflow.com/questions/39070654
复制相似问题