The following versions were used in this post:
Suppose we have a Java Spring-framework based project named “zoo-app”, which has unit tests and integration tests in its app module.
Now, we want to extract the integration tests from “zoo-app” into a separate module, named “zoo-integrationtests” and still use the application.properties from “zoo-app”.
By default, the test searches for properties in its own module (“zoo-integrationtests”*), which will be the test.properties file and not the application.properties file from the tested module (“zoo-app”).
Given this situation, the solution to use the application.properties file and the properties contained in the integration test is the simple addition of initializers = ConfigFileApplicationContextInitializer.class in the @ContextConfiguration annotation of the Test class:
With this class referenced in the @ContextConfiguration, the integration test uses the application.properties file from the application referenced in the classes field, in this case only “ZooApp”.