Search code examples
spring-bootjunitmaven-module

With Spring Boot JUnit tests how do I access a configuration yaml from a different maven module?


I have the following spring boot project, organised in maven modules:

RootModule
--ChildModule1
----ChildModule1_1/src/main/resources/my-config.yml
--ChildModule2
----ChildModule2_1
------src/main/java/mypackage/MyClass.java
------src/test/java/mypackage/MyClassTest.java

MyClass is able to use the properties standing in my-config.yml:

@Configuration
@ConfigurationProperties
public class MyClass {...}

MyClassTest is not able to use those properties, even with the annotations below:

@EnableConfigurationProperties
@TestPropertySource(properties = {"spring.config.location=classpath:my-config.yml"})
@ExtendWith{SpringExtension.class}
public class MyClassTest {...}

When I try to run the tests in my MyClassTest class, it throws an error because it cannot find the my-config.yml in the classpath. If I copy my-config.yml into the below folder, then it works: ChildModule2_1 src/test/resources/my-config.yml

I do not want to place the my-config.yml in src/test/resources, though, since this would mean having two files with the same configurations in the project, which is not a good design decision.

Why does my MyClass find the my-config.yml in the classpath, but not the MyClassTest?


Solution

  • I ended up copying MyClassTest into ChildModule1 and this workaround solved my problem.