Spring + Maven: separate property files for unit tests and integration tests

I am using Spring 2.5.6 and building my project with Maven 2.2.1. We use PropertyPlaceholderConfigurerbeans in Spring to load properties to configure things like a database. Pretty standard stuff. We also have two different test suites: unit tests and integration tests.

I would like to be able to use various property files to configure things like the database URL, differently for two different types of tests. For example, I want unit tests to use tags localhostand integration tests to use a database mydatabase.example.com.

I tried several options where I put the property files in separate subdirectories (one for unit tests and one for integration tests). From there I tried things like the tag additionalClasspathElementsfor maven-failsafe-plugin, but this does not seem to work at all. I tried to use maven-antrun-pluginto copy files to target/classes, but it did not work when I started mvn verify -Dtest=sometest.

I also tried using systemPropertyVariablesmaven to set a property with a name buildEnvironment, which then tried to reference in the Spring bean definition:

<property name="locations">
  <value>classpath:${buildEnvironment}/my-test.properties</value>
</property>

But Spring refused to allow it ${buildEnvironment}. At the moment I have no ideas. I am sure there is a good, easy way to do this, but I cannot figure it out.

Any advice is appreciated.

+3
1

maven:

<build>
  <resources>
    <resource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

<properties>
   <buildEnvironment>yourValue</buildEnvironment>
</properties>

${buildEnvironment} Spring "yourValue" ( src/test/resources/)

+1

Source: https://habr.com/ru/post/1752736/


All Articles