-2

I am using maven sure fire plugin to load the key-value pairs from a properties file (testdata.properties) so that i can use it in my TestNG test method like

@Test
public void testDBConnection()
{ 
    String dbEnvUsed = System.getProperty("db.env");
    String keyForDatabaseDriver = System.getProperty("db.driver");
    String keyForDatabaseUrl = System.getProperty("db."+dbEnvUsed+".url");// value of the key "db.tst.url"
    String keyForDatabaseUser = System.getProperty("db."+dbEnvUsed+".user";//value of the key "db.tst.user"
    String keyForDatabasePassword = System.getProperty("db."+dbEnvUsed+".passwd");//value of the key "db.tst.passwd"
    Connection conn = null;
    try
    {
       Class.forName(dbDriver); // load the database driver
       Connection conn = DriverManager.getConnection(keyForDatabaseUrl, keyForDatabaseUser, keyForDatabasePassword);
            System.out.println("----> Connected to " + dbEnvUsed +" instance");
    } 
    catch(java.sql.SQLRecoverableException sqlre)
    {
            System.out.println("----> Could not establish the connection. DB Server may be down.");
    }
    catch(Exception se)
    {
            System.out.println(se.getMessage());
    }
    Assert.assertNotNull(conn);//test the conection
}

contents of testdata.properties file

db.driver = oracle.jdbc.driver.OracleDriver
db.env= tst

db.tst.url = jdbc:oracle:thin:@hostname:1521:sid
db.tst.user = tstUser
db.tst.passwd = *****

db.dev.url = jdbc:oracle:thin:@hostname:1521:sid
db.dev.user = devUser
db.dev.passwd = ######

Excerpt from my pom.xml

<plugins>
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <systemPropertiesFile>src/main/resources/testdata.properties</systemPropertiesFile>
        </configuration>
    </plugin>
</plugins>

I want to change the value of the key "db.env" before executing the test without having to edit the testdata.properties file. Is there any way to achieve the same ?

Jeeka
  • 861
  • 2
  • 11
  • 21
  • How exactly did you load the properties ? I'm surprised to see you looking up for them on `System`, I would have expected a simple `java.util.Properties` instance, which would allow you to save easily your updated properties by the way – Dici Dec 26 '14 at 22:39
  • maven-surefire-plugin takes care of that. If you specify the path of your properties file within the (see Excerpt from my pom.xml), it would load them up and you can access them in your code via System.getProperty("your.key"). I have found this to be working. – Jeeka Dec 26 '14 at 23:18
  • BTW, i was accessing my properties earlier by creating an instance of java.util.Properties. I switched to using maven-surefire-plugin to way of loading properties as it saved coding a few lines, although it affects the readability of the code (for someone who is not familiar with this usage). – Jeeka Dec 26 '14 at 23:24
  • 1
    Well, I don't know this plugin but I guess that once your properties have been added to the system properties, you cannot distinguish the properties that were already here from those you added later. You can always use `Properties.save` on `System.getProperties` (optionally, you could filter the properties of the system : http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html) – Dici Dec 26 '14 at 23:35

1 Answers1

1

found that maven provides a way to override the value of the of key in your properties file. Below is one way of doing it:

mvn -D<key.to.override>=<yourvalue>

In my case, i did this:

mvn -Dtest= testDBConnection test -Ddb.env=tst

Note that i have a key "db.env" defined in the testdata.properties file which is loaded by the maven-surefire-plugin. So, the value of the key "db.env" can be accessed within my code by using System.getProperty("db.env");.

To make this (loading of your properties file in a way such that your key-value pair can be accessed by using System.getProperty("key.in.your.property")) happen, add the below in your pom.xml

<plugins>
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <systemPropertiesFile>src/main/resources/testdata.properties</systemPropertiesFile>
    </configuration>
</plugin>

Jeeka
  • 861
  • 2
  • 11
  • 21