0

I'm trying to write a custom auto configuration class to override the behaviour of default auto configuration class.

For instance, imagine I'm trying to override the behaviour of org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration. If I name my custom auto configuration class the same as default one like - com.rabbit.RabbitAutoConfiguration, both of them have the same bean name. I use @AutoConfigureAfter in my custom autoconfigure class and the application works fine as expected. However, problem arises when I try to write a test class to test my custom auto configuration class com.rabbit.RabbitAutoConfiguration.

Since both have the same bean name, the order in which, I register these classes really matters while writing test cases.

For instance in test class,

annotationConfigWebApplicationContext.register(org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration.class, com.rabbit.RabbitAutoConfiguration.class);

has different behaviour compared to

annotationConfigWebApplicationContext.register(com.rabbit.RabbitAutoConfiguration.class, org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration.class);

However, I managed to overcome this problem by explicitly specifying bean name to my custom autoconfigure class by specifying @Configuration("customRabbitAutoConfiguration").

Shouldn't the class names be same? If so, why is that I see this problem only when writing test class and not when using the real application.

Just wondering is there a better way to do it.

Manikdn84
  • 357
  • 4
  • 21
  • Because the application doesn't load the `RabbitAutoConfiguration` as that is bound to certain conditions in your test you don't honor those. – M. Deinum Oct 05 '17 at 06:45
  • @M. Denium, Thanks for your response!! I don’t quite understand your comment. Can you please be more elaborative. Thanks – Manikdn84 Oct 05 '17 at 14:26
  • What isn't clear about it? When using auto configuration the Spring Boot `RabbitAutoConfiguration` won't be added due to all the `@ConditionalOn` annotations due to your own `RabbitAutoConfiguration`. This doesn't apply when you create your own context and basically the last one wins. – M. Deinum Oct 05 '17 at 17:10
  • Sorry for the late reply, as I was away!! Thanks for your reply @Denim. Makes perfect sense. – Manikdn84 Nov 07 '17 at 21:33

0 Answers0