0

In my application I have three different profiles and their own property files. In my application.properties file the correct active profile will be set by the profile of my maven build:

spring.profiles.active=@profileActive@

The maven profiles are dev, test and prod. The maven command looks like that: mvn clean install -Pdev for example. After the build, my active profile is set to dev.

Now, I added also some property files to my test/resource folder to use @TestPropertySource for my tests. The application.properties in this folder has the same content as the normal one.

My problem is now, that I the @profileActive@ in application.properties in my test folder doesnt set the profile of the maven command. So my tests are red because no profile is set and the context could not be loaded.

Is there any way to get the profile set automatically in the application.properties in test/resource by using the same maven command?

Patrick
  • 12,336
  • 15
  • 73
  • 115
  • Just curious, why would you test a production profile (or any environment-specific profile, for that matter) in your unit tests? – Alex Savitsky Dec 15 '17 at 13:43
  • @AlexSavitsky for testing the db connection for example. Or to test functionalities because of conditions. – Patrick Dec 15 '17 at 13:46
  • @CowboyFarnz sure I can do that but I wont. It should work automatically. – Patrick Dec 15 '17 at 14:10
  • @Patrick Another way to do this is to define a base (abstract) test class that your actual test classes will extend. This allows you to extract more than just the '@ActiveProfiles' annotation. You could also imagine more specialised base classes for different kinds of integration tests, e.g. data access layer vs service layer, or for functional specialties (common '@Before' or '@After methods' etc). – Cowboy Farnz Dec 15 '17 at 14:17
  • Try adding `test/resource` to the resource filtering directory as described in https://stackoverflow.com/a/42391322/1398418 – Oleg Dec 15 '17 at 14:25
  • Just like Oleg already said, make sure that the `test/resource` directory is also included for filtering. The value should be replaced just like in the production resources. – Daniel Olszewski Dec 18 '17 at 10:55

0 Answers0