首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring-由于未绑定RestClientTest,启动MockRestServiceServer无法正确自动配置RestTemplate

Spring-由于未绑定RestClientTest,启动MockRestServiceServer无法正确自动配置RestTemplate
EN

Stack Overflow用户
提问于 2016-08-22 10:34:35
回答 2查看 14.5K关注 0票数 16

编辑:这个问题特别与spring-boot 1.4.0中引入的@RestClientTest注释有关,该注释旨在替换工厂方法。

问题:

根据documentation,@RestClientTest应该正确配置MockRestServiceServer,以便在测试REST客户端时使用。但是,在运行测试时,我收到一个IllegalStateException,显示MockServerRestTemplateCustomizer尚未绑定到RestTemplate。

值得注意的是,我使用Gson进行反序列化,而不是Jackson,因此排除。

有人知道如何正确使用这个新的注解吗?我没有发现任何需要更多配置的示例,而我已经这样做了。

配置:

代码语言:javascript
运行
复制
@SpringBootConfiguration
@ComponentScan
@EnableAutoConfiguration(exclude = {JacksonAutoConfiguration.class})
public class ClientConfiguration {

...

   @Bean
   public RestTemplateBuilder restTemplateBuilder() {
       return new RestTemplateBuilder()
            .rootUri(rootUri)
            .basicAuthorization(username, password);
   }
}

客户端:

代码语言:javascript
运行
复制
@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();
    }
}

测试

代码语言:javascript
运行
复制
@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));
    }
}

但有一个例外:

代码语言:javascript
运行
复制
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使用的过滤器)

代码语言:javascript
运行
复制
@ComponentScan(excludeFilters = {
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)
})
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-22 12:22:58

你在两个地方有RestTemplateBuilder。在ClientConfiguration类和ComponentsClientImpl类。Spring boot 1.4.0自动配置可用于在需要时创建RestTemplate实例的RestTemplateBuilder。从ClientConfiguration类中删除以下代码并运行测试。

代码语言:javascript
运行
复制
 @Bean
 public RestTemplateBuilder restTemplateBuilder() {
   return new RestTemplateBuilder()
        .rootUri(rootUri)
        .basicAuthorization(username, password);
  }
票数 5
EN

Stack Overflow用户

发布于 2016-08-22 12:35:45

应该使用RestTemplate从静态工厂构造MockRestServiceServer实例。有关测试过程的详细描述,请参阅this文章。

在您的示例中,您可以执行以下操作:

代码语言:javascript
运行
复制
@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*/
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39070654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档