Java maven Junit read systemproperties variable to load property file - java

Java Maven Spring Junit with webapplication
I am using following code to load property file into spring context placer holder.
<context:property-placeholder location="file:${RESOURCE_PATH}/jdbc.properties" />
in eclipse Junit run time configuration i have defined "RESOURCE_PATH" so it runs fine when i execute my junit tests from GUI but when i run from maven they fail.
Can we define variable and pass in pom file at run time ?

You should either supply the property RESOURCE_PATH using -D switch when running maven or put it into pom.xml into section <properties>; something like this:
<properties>
<RESOURCE_PATH>put your path here</RESOURCE_PATH>
</properties>

Related

Spring Boot Maven Build Problem with Random Values in application.properties

I have edited, for example, application.properties from
spring.mail.host=stmp.test.com
to
spring.mail.host=${server.mail.host}
and I override at starttime these properties to the correct values. This works fine until I want to run maven to build my application.
I receive the following Exception
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class
I think the problem is that maven also needs these values but how and where can I insert them? I dont want to run mvn on the cli.
I think #Nicholas K was making the point that you can pass in those values in you maven command. For example with the argument mvn spring-boot:run "-Dserver.mail.host=mailhost".
You can also set an environment variable and that should be injected:
export SERVER_MAIL_HOST=mailhost
Or if you sometimes don't want to set them you could set a default for the property in your properties file:
spring.mail.host=${server.mail.host:defaultmailhost}
Or default it to an empty string

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?
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.
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=...
Edit from april 2021
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
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"
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.
E.g.: mvn test -Dspring.profiles.active=dev
You can add as many profiles as needed.
syntax: application-<profile name>.properties
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 "_".

Jenkins Gradle test fails on Commons Configuration

I have a project that uses Apache Commons Configuration. The project is built using gradle. I have some unit test cases written on this project and gradle test works fine when run locally.
However when the unit tests are run in Jenkins it fails.
Please see the screenshot of the error. The error seems to be to do something with Commons Configuration that I am using. Please help.
org.apache.commons.configuration.ConfigurationRuntimeException: No ConfigurationProvider registered for tag disabledAdministrativeMonitors
org.apache.commons.configuration.ConfigurationException: org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: No ConfigurationProvider registered for tag disabledAdministrativeMonitors
at org.apache.commons.configuration.DefaultConfigurationBuilder.createConfigurationAt(DefaultConfigurationBuilder.java:752) ~[commons-configuration-1.6.jar:1.6]
at org.apache.commons.configuration.DefaultConfigurationBuilder.initCombinedConfiguration(DefaultConfigurationBuilder.java:628) ~[commons-configuration-1.6.jar:1.6]
at org.apache.commons.configuration.DefaultConfigurationBuilder.getConfiguration(DefaultConfigurationBuilder.java:560) ~[commons-configuration-1.6.jar:1.6]
The ConfigurationProvider try to load a configuration xml file which is default the config.xml.
In my case the project is build using Jenkins. Jenkins provides a config.xml in Jenkins home dir. This is loaded first instead of my desired one. Maybe that applies for you too?
Example Jenkins config.xml
<?xml version=’1.1' encoding=’UTF-8'?>
<hudson>
<disabledAdministrativeMonitors>
...
</disabledAdministrativeMonitors>
...

Does start-class take any argument to supply a value for main class?

I got myself up and running with Spring, maven using Spring Boot. You may check the below link for details -
https://ashikuzzaman.wordpress.com/2015/06/04/spring-with-maven-using-spring-boot/
In my pom.xml I have the following properties defined.
<properties>
<java.version>1.7</java.version>
<!-- The main class to start by executing java -jar -->
<start-class>com.github.ashikuzzaman.javaapichecks.spring.RawLinkedListTypes</start-class>
</properties>
I wanted to pass one or more runtime arguements to get into the arguement list that main() accepts. Can I do this via a parameter passing in start-class or main-class?
Suppose you are trying to give some value for java.version while you are using mvn package command from terminal. Then you can use it -
mvn package "-Djava.version=1.7"

Maven - use different java classes during 'test' and 'war' phase

I'm using maven war plugin to build war package.
Before package is build test are executed. To preinitialize my database with sample data I use spring bean. I would like to have different data in my db for tests and different when application starts.
I was thinking that maybe it is possible to use two different spring initializer classes in 'test' and 'war' phases but I don't know how to achieve this.
You have to put the different classes you need into src/main/java or src/test/java or may be supplemental application.xml into src/main/resources or src/test/resources. The test initializer can be done by a Test class which initializes first before all tests are running (take a look at testng which has this kind of feature).
Your tests should not be using the production Spring context (xml) files.
Instead, if you need to access an ApplicationContext in your tests (or if you are using a base testcase class like AbstractTransactionalJUnit4SpringContextTests), set up a test-context.xml context which points to the test database configuration and the test data scripts.

Categories

Resources