2

Small question regarding a Spring profile (integration) applied to Springboot please.

Currently, under src/main/resources, I have several application.properties files. Such as:

  • application properties
  • application-local.properties
  • application-production.properties
  • application-integration.prorperties

And under the src/test/resources, no properties file.

When I run maven, maven will trigger some unit and integration tests. The unit test does not need any particular profile (properties are set in the unit tests, anyway, it is not the question).

The integration tests, they need the application-integration.properties. And currently, with the file under src/main/resources, everything is fine.

I just tried moving the file to src/test/resources, and the integration tests could not find any property, as if the file disappeared.

What is the proper way to tell Springboot to run integration tests with a application-integration.properties under src/test/resrouces please?

Thank you

Sri
  • 437
  • 1
  • 4
  • 13
PatPanda
  • 3,644
  • 9
  • 58
  • 154
  • 2
    Spring Boot doesn't know anything about src/test/resources or src/main/resources. It loads files using the classpath instead. It's your build system or IDE's job to ensure that files that were in src/main/resources or src/test/resources are on the classpath and that sounds like it's not happening in this case. The possible causes for that will depend on the build system or IDE that you're using an how you're running your tests. – Andy Wilkinson Jan 18 '21 at 20:21
  • Understood. I am currently building in with Maven as part of a Jenkins job, no IDE involved. Is there a way to simply tell "hey, when you run integration tests, look at the application-integration.properties that is found under /src/test/resources"? – PatPanda Jan 20 '21 at 02:18
  • Your question is not really clear. I suggest you provid a code snippet to make your question more precise. Anyway if all you want to do is injecting Value in a component's property you do not need spring context in such test lets say you have a service class @Service public class MyService { @Value("${my.super.property}") String property ; public String doSomething () { return property; } } And you want to write test (lets say a unit test) and you want to inject a value in your property field. – soung Feb 12 '21 at 03:40
  • You can use [`ReflectionTestUtils`](http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/util/ReflectionTestUtils.html) – soung Feb 12 '21 at 03:40
  • 2
    Hello soung, I believe my question was clear enough, Sri provided the correct answer already – PatPanda Feb 12 '21 at 04:53

1 Answers1

0

You can use the below configuration for the Integration tests

@TestPropertySource(locations = "classpath:application-integration.properties")
@ActiveProfiles("integration")
Sri
  • 437
  • 1
  • 4
  • 13
  • I tried this but for me the custom properties defined in application-integration.properties file aren't being read in the test class. I have this file defined in src/test/resources folder. What else could be going wrong? – Andy Dufresne Feb 09 '22 at 11:19