Reputation:
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
?
Upvotes: 2
Views: 13097
Reputation: 611
I don't see many people using the environment variable option. If you set an environment variable for corresponding properties, then the value in the environment variable will be used. e.g.
Environment variables: SPRING_DATASOURCE_URL="jdbc:postgresql://my.test.server.com:5432/myDatabase" SPRING_DATASOURCE_USERNAME=anotherUsername
Inside the properties file: spring.datasource.url=jdbc:postgresql://localhsost:5432/myDatabase spring.datasource.username=myUsername
The application will use the values in the environment variables. For this to work you'll need to follow the naming convention. Use uppercase and replace "." with "_".
Upvotes: 0
Reputation: 3169
When overriding parameters in the command line, use a comma as separator, not a space:
mvn test -Drun.arguments='--spring.datasource.url=...,--spring.datasource.username=...'
This should work too:
mvn test -Dspring.datasource.url=... -Dspring.datasource.username=...
The syntax above was valid for Spring Boot 1.X. With Spring Boot 2.0/2.1, use:
mvn test -Dspring-boot.run.arguments='--spring.datasource.url=...,--spring.datasource.username=...'
And with Spring Boot 2.2, the syntax was changed again (use a whitespace as separator):
mvn test -Dspring-boot.run.arguments='--spring.datasource.url=... --spring.datasource.username=...'
Other answers and comments mention using profiles and put a custom application.properties
in /src/test/resources
, which is not a viable solution for you since you use different pipelines, but if I remember correctly, you can even use application-{profile}.properties
in /src/test/resources
. This way you should be able to maintain one test profile per pipeline, where you put your custom parameters, and then test your pipeline with:
mvn test -Dspring.profiles.active=foobar
Upvotes: 4
Reputation: 21391
Option 1 (preferred as is Maven structure-specific)
Create an application.properties
under the test/resources
to be picked up for your testing purposes
Option 2 (Spring Test fine-tuning a particular Test class alone)
Override your properties directly on the Test class by inlining the ones you want by using @TestPropertySource
Option 3 (Spring Boot - multiple properties files or a single YAML file)
Group the props under a Spring Profile (Example here) and invoke it directly from maven: mvn test -Dspring.profiles.active="myOtherSpringProfile"
Upvotes: 1
Reputation: 423
Create another application-dev.properties
file and paste:
spring.datasource.url=jdbc:postgresql://my.test.server.com:5432/myDatabase
spring.datasource.username=anotherUsername
Then run with the option -Dspring.profiles.active=dev
in your mvn
command.
mvn test -Dspring.profiles.active=dev
You can add as many profiles as needed.
application-<profile name>.properties
Upvotes: 1
Reputation: 15308
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.
Upvotes: 5