Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the order of initialization of the configuration classes in @ContextConfiguration be influenced?

Tags:

java

spring

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.

like image 363
allprog Avatar asked Oct 15 '13 11:10

allprog


People also ask

What is @ContextConfiguration in Junit?

@ContextConfiguration defines class-level metadata that is used to determine how to load and configure an ApplicationContext for integration tests.

Which annotation will set ApplicationContext for the test class with the configuration file from class path specified?

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.

What is @configuration spring boot?

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.

Which annotation will set ApplicationContext for the test class?

Set the spring ApplicationContext for your test classes using @ContextConfiguration annotation.


1 Answers

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.)

like image 96
allprog Avatar answered Oct 04 '22 07:10

allprog