vendredi 4 janvier 2019

Wiring a Test in Spring Boot 2.1 without enabling bean overriding

I have code with named beans

@Bean
@Named("heimdall-uri-supplier")
public URISupplier heimdallEndpointSupplier(CredentialsClientConfig config, EnvInfo envInfo) {
    ....
}

@Named("vault-uri-supplier")
@Bean
public URISupplier vaultURISupplier(EnvInfo envInfo, CredentialsClientConfig config) {
   ....
}

They are explicitly Named because I want specific implementations injected into different consuming classes. These classes also use @Named.

In tests, prior to Spring Boot 2.1

@Bean
@Primary
@Named("heimdall-uri-supplier")
public URISupplier heimdallEndpointSupplier(CredentialsClientConfig config, EnvInfo envInfo) {
    return mock of some sort
}

@Named("vault-uri-supplier")
@Bean
@Primary
public URISupplier vaultURISupplier(EnvInfo envInfo, CredentialsClientConfig config) {
   return mock of some sort
}

worked great.

Now, of course spring boot 2.1 disables overriding. I know I can reenable it, but I'd rather not in theory do so.

But my "normal" workaround (do @Bean(name="testFoo") won't work here because the @Named injector in the consuming classes will now fail.

Are there any solutions?

Aucun commentaire:

Enregistrer un commentaire