Reuse application.yml for tests

Viewed 66

in Spring Boot there are usually two application.yml files, in src/main/java/resources and src/test/java/resources. The first one will be used in when the program is run, the second one when the tests are run.

We had some errors in the past that were introduced by adding something in the main yml file but weren't manually copied over to the test yml. Therefore the tests still worked (since they worked on the old yml) but the main application would not. This is unlikely but still the fact you need to manually sync those doesn't feel good to me.

So what I would like to do is only to have the main application.yml with an additional test profile, that would load the test overrides for the unit tests.

I had two angles on how to achieve this:

  1. Overriding the classloader to load the main yml in the the tests in a @Configuration or try to additionally load the main yml. This didn't work since src/main/java/resources is not on the classpath when the tests are run. Careful, you could load the file, this works when the tests are run from source, but doesn't if the application is packaged in a jar.
  2. A gradle task that runs before the tests and copies the main yml to the test resources. This works, but I don't like it since it is intransparent.

Is there any canonical way to do this or is there any reason of why this is a stupid idea in the first place and shouldn't be done? For me it would seem, because Spring works so well with profiles, that a single application.yml with different profiles for dev, test, e2e, ... would be the sensible default since it is a single point of truth.

0 Answers
Related