Search code examples
javaspringmavenapplication.properties

mvn test - override values in application.properties


I have these properties in my application.properties:

spring.datasource.url=jdbc:postgresql://localhsost:5432/myDatabase
spring.datasource.username=myUsername

I would like to run mvn test with other values than the above, for example:

spring.datasource.url=jdbc:postgresql://my.test.server.com:5432/myDatabase
spring.datasource.username=anotherUsername

I tried the following

mvn test -Drun.arguments='--spring.datasource.jdbc:postgresql://my.test.server.com:5432/myDatabase --spring.datasource.username=anotherUsername'

and without spring prefix:

mvn test -Drun.arguments='--datasource.jdbc:postgresql://my.test.server.com:5432/myDatabase --datasource.username=anotherUsername'

But this does not seem to work. How can I override the values in the application.properties in context of running mvn test?


Solution

  • Something like this should work:

      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        <configuration>
          <systemPropertyVariables>
            <spring.datasource.jdbc>value</spring.datasource.jdbc>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    

    But more often we do this by placing a test version of application.properties into the src/test/resources. During testing, that file will have greater priority.