使用 @RestClientTest 对休息客户端进行 Spring 启动测试 [英] Spring boot testing of a rest client using @RestClientTest

查看:154
本文介绍了使用 @RestClientTest 对休息客户端进行 Spring 启动测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring boot 1.5.8 并想测试我的客户端:

I am using spring boot 1.5.8 and want to test my client:

@Component
public class RestClientBean implements RestClient {
  private Map<String, RestTemplate> restTemplates = new HashMap<>();

  @Autowired
  public RestClientBean(RestTemplateBuilder builder, SomeConfig conf) {
    restTemplates.put("first", builder.rootUri("first").build();
    restTemplates.put("second", builder.rootUri("second").build();
  }
}

进行以下测试:

@RunWith(SpringRunner.class)
@RestClientTest(RestClient.class)
public class RestClientTest {
  @Autowired
  private RestClient client;

  @Autowired
  private MockRestServiceServer server;

  @TestConfiguration
  static class SomeConfigFooBarBuzz {
    @Bean
    public SomeConfig provideConfig() {
        return new SomeConfig(); // btw. not sure why this works, 
                                 // but this is the only way 
                                 // I got rid of the "unable to load 
                                 // SomeConfig auto-wire" or something like this :)
                                 // Anyway not my main issue
                                 // EDIT: just realized that the whole 
                                 // @TestConfiguration part can be avoided by
                                 // adding SomeConfig.class to the classes in the
                                 // @RestClientTest annotation
    }
  }

  @Before
  public void setUp() throws Exception {
    server.expect(requestTo("/somePath")) // here an exception is thrown
                                          // (main issue)
          .andRespond(withSuccess("<valid json>", MediaType.APPLICATION_JSON));
  }
}

异常很明显:

java.lang.IllegalStateException: Unable to use auto-configured 
MockRestServiceServer since MockServerRestTemplateCustomizer has been bound to 
more than one RestTemplate

但是是否有可能以某种方式对此进行测试,或者是否不允许在一个客户端类中实例化两个不同的休息模板?我只需要在某些情况下使用第一个休息模板,而在其他情况下使用第二个模板.

But is it somehow possible to get this tested or is it not allowed to instantiate two different rest templates in one client class? I have just the need to use the first rest template in some cases and the second one in some others.

推荐答案

经过几天的调查并通过 GitHub 与 spring 人员沟通后,我找到了一个适合我的解决方案,但在这里没有收到答案意味着我的解决方案可能对某人有价值:

After some days of investigation and communication with spring folks via GitHub I found a solution for me and not receiving an answer here means my solution might be valuable for someone:

@RunWith(SpringRunner.class)
@RestClientTest(RestClient.class)
public class RestClientTest {
  @Autowired
  private RestClient client;

  private MockRestServiceServer firstServer;
  private MockRestServiceServer secondServer;
  private static MockServerRestTemplateCustomizer customizer; 

  @TestConfiguration
  static class RestTemplateBuilderProvider {
    @Bean
    public RestTemplateBuilder provideBuilder() {
      customizer = new MockServerRestTemplateCustomizer();
      return new RestTemplateBuilder(customizer);
    }
  }

  @Before
  public void setUp() {
    Map<RestTemplate, MockRestServiceServer> servers = customizer.getServers();
    // iterate and assign the mock servers according to your own strategy logic
  }

  @Test
  public void someTest() {
    firstServer.expect(requestTo("/somePath"))
               .andRespond(withSuccess("some json body"), 
                           MediaType.APPLICATION_JSON));

    // call client

    // verify response
  }

基本上指定与您在客户端代码中使用的休息模板数量相同的模拟服务器数量,然后指定一个测试配置,提供带有定制器的休息构建器,以便您的客户端代码的休息模板将通过此定制构建建设者.然后使用定制器将模拟服务器绑定到创建的其余模板,并根据需要定义对它们的期望.

Basically specify a number of mock servers same as the number of rest templates you use in your client code, then specify a test configuration providing a rest builder with a customizer, so that your client code's rest templates will be built via this customized builder. Then use the customizer to get the mock servers bounded to the created rest templates and define expectations on them as you want.

这篇关于使用 @RestClientTest 对休息客户端进行 Spring 启动测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆