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.