美好的一天,我正在尝试编写 Feign 特定的集成测试并具有特定的配置以启用 feign 自动配置,看起来像这样
@Profile({Profiles.FEIGN_INTEGRATION_TEST, Profiles.PROD, Profiles.DEV, Profiles.STAGE})
@Configuration
@EnableFeignClients
public class FeignEnabler {
}
在我的测试中(Spock 规范)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWireMock
@ActiveProfiles(Profiles.FEIGN_INTEGRATION_TEST)
class MyServiceSpec extends Specification {
@Subject
@Autowired
MyService myService
}
MyService
正在使用RestClient
@Profile({Profiles.FEIGN_INTEGRATION_TEST, Profiles.PROD, Profiles.DEV, Profiles.STAGE})
@FeignClient(name = "myRestClient", url = "${feign.client.config.myclient.url}")
public interface RestClient
可悲的是,出现了运行测试规范异常
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.myapplication.web.RestClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
解决该问题的方法是添加@EnableFeignClients
类@SpringBootApplication
,但这样会与另一个测试(在我的情况下为 DataJpa 切片测试)混淆,为了避免它,我需要添加这行代码
@ImportAutoconfiguration({RibbonAutoConfiguration.class, FeignRibbonClientAutoConfiguration.class, FeignAutoConfiguration.class})
如此处所述https://github.com/spring-projects/spring-boot/issues/7270用于每个集成测试,不使用 FeignAutoConfiguration 我真的不想这样做......