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.

zoo-app-01

  @SpringBootApplication
  public class ZooApp {
      public static void main(String[] args) {
          SpringApplication.run(ZooApp.class);
      }
  }

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

zoo-app-02

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:

@ContextConfiguration(
  initializers = ConfigFileApplicationContextInitializer.class,
  classes = {ZooApp.class})
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class ZooAppIntegrationTest {

  @Test
  public void testZooAppIntegration() {
    // some integration tests here
  }
}

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

Answer also featured on stackoverflow: link1 link2.