David Sackstein
David Sackstein

Reputation: 570

How can I inject properties for test and for main into each sub module of a multi-module gradle spring boot project?

This is the structure of my project.

parent
      | build.gradle
      | settings.gradle
      | [no code]
child1
      | build.gradle
      | src/
          | main/
              | java/
              | resources/
                  | application.properties
          | test/
              | java/
              | resources/
                  | application.properties
child2
      | build.gradle
      | src/
          | main/
              | java/
          | test/
              | java/
child3
      | build.gradle
      | src/
          | main/
              | java/
          | test/
              | java/

I have a property called "datapath" that I would like to inject into classes in each of the modules.
There are two possible values for "datapath", one for tests and one for production.
I set the production value in child1/src/main/resources/application.properties and the test value in child1/src/test/resources/application.properties

I have tried creating configuration classes and specifying PropertySource. But the result has been that though child1 picks up the correct application properties in both test and main, spring does not find them in other modules.

Can you propose a strategy for me to implement this?

In particular:

  1. The tests in the child1 are annotated SpringBootTest but preferably child2 and child3 should not depend on spring boot (just spring framework for autowiring)
  2. I would like to be able to use the @Value annotation on configuration classes in the child modules.
  3. How do I direct spring to resolve these properties from the application.properties in the child1 module, using the one in src/test/resources for tests and the one in src/main/resources for production?
  4. As I have chosen a very "classical" structure, I would like to be able to achieve this with as few as possible moving parts. In particular I would prefer not to have to specify paths explicitly in annotations.

Upvotes: 1

Views: 1081

Answers (1)

lczapski
lczapski

Reputation: 4140

I assume that in child2 and child3 you need application.properties only for test. Then in test you can use @TestPropertySource where you can point relative path to properties file in child1 or add datapath explicitly:

@TestPropertySource(properties = { "datapath=value" })
public class Child2Test {

Upvotes: 1

Related Questions