【问题标题】:Spring Feign configuration: how to test all @Bean methods are calledSpring Feign配置:如何测试所有@Bean方法被调用
【发布时间】:2019-11-12 03:27:22
【问题描述】:

我想用一个配置类来配置Spring feign,并且我想确保当Spring为我配置feign客户端时,所有的@Bean方法都被调用了。

如何测试?

例如,我有:

@FeignClient(
        name = "PreAuthSendRequest",
        url = "${xxx.services.preauth.send.url}",
        configuration = AppFeignConfig.class)
public interface RequestService {
    @PostMapping("")
    @Headers("Content-Type: application/json")
    PreAuthResponse execute(@RequestBody PreAuthRequest preAuthRequest);
}

还有AppFeignConfig.java:

@Configuration
@RequiredArgsConstructor
public class AppFeignConfig{

    private final HttpClient httpClient;
    private final Jackson2ObjectMapperBuilder contextObjectMapperBuilder;

    @Bean
    public ApacheHttpClient client() {
        return new ApacheHttpClient(httpClient);
    }

    @Bean
    public Decoder feignDecoder() {
        return new JacksonDecoder((ObjectMapper)contextObjectMapperBuilder.build());
    }

    @Bean
    public Encoder feignEncoder() {
        return new JacksonEncoder((ObjectMapper)contextObjectMapperBuilder.build());
    }

    @Bean
    public Retryer retryer() {
        return Retryer.NEVER_RETRY;
    }


    @Bean
    public ErrorDecoder errorDecoder() {
        return new ServiceResponseErrorDecoder();
    }
}

那么,如何验证是否调用了所有@Bean 方法?我知道@MockBean,但是我要查的是config.feignDecoder()等,确实被调用了。

当我尝试context.getBean(RequestService.class); 并调用execute() 方法时,它似乎发送了一个真正的请求并且没有wiremock,它显然失败了。

【问题讨论】:

  • 这看起来很像测试具体的实现和框架。您要测试的行为是什么?
  • 所以你的意思是,这是与实现相关的,与行为无关?或者,这部分,获取正确的 bean,不是为了测试?
  • 我觉得在这些方法中的每一个中放置一条日志消息(带有 DEBUG 级别)将是确保调用它们(而不是测试它们)的好方法
  • 是的 @ali4j 这也可能是一个很好的解决方案。我正在考虑这个,但我不知道;将标准输出重定向到一个文件并检查在这个项目中已经完成了几次的开销。也许吧。
  • 是的 @ali4j 这也可能是一个很好的解决方案。我正在考虑这个,但我不知道;将标准输出重定向到一个文件并检查在这个项目中已经完成了几次的开销。也许吧。

标签: java spring mocking spring-cloud-feign


【解决方案1】:

现在我有这个:

@SpringBootTest
@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
class RequestServiceTest {

    @Autowired
    private ApplicationContext applicationContext;

    @MockBean
    private ApacheHttpClient client;

    @MockBean
    private Decoder feignDecoder;

    @MockBean
    private Encoder feignEncoder;

    @MockBean
    private Retryer retryer;

    @MockBean
    private ErrorDecoder errorDecoder;

    @Test
    void shouldRetrieveBeansFromApplicationContextToConstructConfigurationInstance() {
        AppFeignConfig config = applicationContext.getBean(AppFeignConfig.class);
        Assertions.assertEquals(config.feignEncoder(), feignEncoder);
        Assertions.assertEquals(config.feignDecoder(), feignDecoder);
        Assertions.assertEquals(config.errorDecoder(), errorDecoder);
        Assertions.assertEquals(config.client(), client);
        Assertions.assertEquals(config.retryer(), retryer);
    }

}

不知道是不是应该这样。如果有任何想法,请发表评论。

【讨论】:

  • 如果您认为这不是应该做的事情,尤其是当您投反对票时,请理性评论。
猜你喜欢
  • 2018-10-08
  • 2020-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 2016-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多