Search code examples
javaspringintegration-testingmulti-module

Spring integration test module running app from separate module without properties for DB config


Given a spring boot gradle module named "md-core" with a Spring application runner and a PostgresDbConfig to connect it to the DB.

The app runs perfectly fine and resolves the properties from the "application.yml" file in the @Config.

@Value("${db.default.pool.size}")
Integer maxPoolSize;

Now, a separate module called "integrationtests" tries to launch the Spring Boot Runner in the "md-core" module.

@ContextConfiguration(classes = {MdCore.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class GivenTest

The test launches the "md-core" Spring runner, but when trying to resolve the properties in the @Config, it does not find any properties.

I've tried directly resolving the "application.yml" in the test. It does not send the properties over to the "md-core" module. Any attempt of adding the "application.yml" properties in the test resolves them to the test file, but does not send them over when the "md-core" module is accessed.

Are there any definitions which should be given in the test via annotations or in the gradle build files?

The classpath when launching the test does not contain the "md-core" resources location, only the classes.

Could this be a reason? If yes, how can the resources be referenced in the classpath of the gradle build files?

I apologize for any mistakes or incomplete information, this post is being written at the end of a work day, hoping there will be answers by morning.


Solution

  • Given this situation, the solution to use the application.properties file for the integration test is the simple addition of initializers = ConfigFileApplicationContextInitializer.class in the @ContextConfiguration annotation of the Test class:

    @ContextConfiguration(
      initializers = ConfigFileApplicationContextInitializer.class,
      classes = {SomeApp.class})
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    public class SomeIntegrationTest {
    
      @Test
      public void testTheIntegration() {
        // some integration tests here
      }
    }
    

    Answer also documented in a post on my blog and initially found less detailed on another stackoverflow question.