Daniel Lavoie
Daniel Lavoie

Reputation: 1922

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. The following snippet comes from the sources of 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.

Considering I am running JUnit integration tests, that kind of event won't occur.

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.

Here's the Test configuration class.

@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 ? (That is the only work around I have found yet).

Upvotes: 0

Views: 1693

Answers (1)

Patrick Grimard
Patrick Grimard

Reputation: 7126

So as discovered, you need to annotate your class with @WebIntegrationTest.

Upvotes: 3

Related Questions