简体   繁体   English

如何通过Spring Boot Integration测试中的@RefreshScope初始化bean?

[英]How can I initialize a bean with @RefreshScope from Spring Boot Integration tests?

I've been scratching my head for a couple of hours. 我已经挠头了几个小时。

Spring Cloud Netflix autoconfigures an Eureka client. Spring Cloud Netflix自动配置Eureka客户端。 The following snippet comes from the sources of EurekaClientAutoConfiguration. 以下代码片段来自EurekaClientAutoConfiguration的来源。

@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
@org.springframework.cloud.context.config.annotation.RefreshScope
@Lazy
public EurekaClient eurekaClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, EurekaInstanceConfig instance) {
  applicationInfoManager.getInfo(); // force initialization
  return new CloudEurekaClient(applicationInfoManager, config,
                    this.optionalArgs, this.context);
        }

This bean will only be initialized when my Spring Boot application triggers an application event. 仅当我的Spring Boot应用程序触发应用程序事件时,才会初始化此bean。

Considering I am running JUnit integration tests, that kind of event won't occur. 考虑到我正在运行JUnit集成测试,这种事件将不会发生。

Here the unit test : 这里的单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ReferentialClientTest.class)
public class InstrumentClientIT {
    @Resource
    private InstrumentClient instrumentClient;

    @Test
    public void testInstrumentClient() {
        instrumentClient.findOne(455540l).getName();
    }
}

The InstrumentClient is a Feign client that depends on EurekaClient. InstrumentClient是依赖于EurekaClient的Feign客户。

Here's the Test configuration class. 这是Test配置类。

@EnableFeignClients
@EnableDiscoveryClient
@EnableAutoConfiguration
public class ReferentialClientTest {
    public static void main(String[] args) {
        SpringApplication.run(ReferentialClientTest.class, args);
    }
}

How can I make sure that the EurekaClient is properly initialized without wiring it into my integration test ? 如何确保EurekaClient正确初始化而不将其连接到集成测试中? (That is the only work around I have found yet). (这是我发现的唯一解决方法)。

因此,正如发现的那样,您需要使用@WebIntegrationTest注释类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 spring boot + cloud 中禁用 refreshScope 运行状况指示器? - How can I disable the refreshScope health indicator in spring boot + cloud? 如何使用@RefreshScope刷新Spring Integration Poller? - How to refresh spring integration poller with @RefreshScope? 如何在 AWS 上运行的 Spring Boot 应用程序上本地运行我的集成测试? - How can I run my integration tests locally on a Spring Boot application running on AWS? 如何为所有Spock Integration测试模拟部分Spring Boot服务? - How can I mock part of a Spring Boot service for ALL of my Spock Integration tests? @RefreshScope无法正常工作 - Spring Boot - @RefreshScope not working - Spring Boot 无法在Spring Boot中初始化Bean - Unable to initialize bean in spring boot 如何将 spring bean 添加到 Spring Boot 测试类? - How can I add a spring bean to a Spring Boot test class? 如何在Spring Boot中配置集成测试 - How to configure integration tests in Spring Boot 在Spring引导测试中,如何创建一个bean并通过@Autowired注入? - In a Spring boot test, how can I create a bean and injects by @Autowired? 如何在Spring-Boot中获取属性文件的bean? - How can I get the bean of a property file in Spring-Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM