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 ?