I'm using the @ContextConfiguration
annotation to manage configurations in my application. The configurations are created so that they provide only the beans that are exposed by that given module. For this reason, some beans that are used by the given module are not necessarily imported directly. Example:
configuration --(use)--> module1 --(cannot @Import)--> database
\-(use)--------------------------------> database
In words, the configuration
uses module1
which requires (but must not directly import) the database configuration. Therefore, configuration
uses the database
module as well.
But it seems like the order in which the imports are resolved is quite random. Even if I use
@ContextConfiguration(classes={DatabaseConfig.class, Module1Config.class})
This results in indeterministic failure on initialization (NoSuchBeanDefinitionException
).
Is there any way to influence the order in which the beans are initialized? Or should I create an overlay of configurations that @Import
the configurations along the dependencies? But in that case the same question applies to @Import
as it has to ensure the order in which the dependencies are loaded.
@ContextConfiguration defines class-level metadata that is used to determine how to load and configure an ApplicationContext for integration tests.
At its core, the TestContext framework allows you to annotate test classes with @ContextConfiguration to specify which configuration files to use to load the ApplicationContext for your test.
Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.
Set the spring ApplicationContext for your test classes using @ContextConfiguration annotation.
This issue seems to have stemmed from the different versions of spring being available at the same time. When the code was left to run, only a fraction of the @Imports
were loaded by the org.springframework.context.annotation.ConfigurationClassParser.collectImports(AnnotationMetadata, Set<Object>, Set<Object>)
method. When the execution was suspended by a breakpoint during the parsing, everything worked completely fine.
As soon as the multiple versions of the spring libs were cleaned up, the issue went away. (At least it has not appeared again after a dozen or so runs.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With