如何更改maven surefire插件加载的属性文件中的密钥值,而不必手动编辑该文件?
我正在使用maven sure fire插件从属性文件(testdata.properties)加载键值对,以便在我的TestNG测试方法中使用它,如如何更改maven surefire插件加载的属性文件中的密钥值,而不必手动编辑该文件?,maven,maven-surefire-plugin,Maven,Maven Surefire Plugin,我正在使用maven sure fire插件从属性文件(testdata.properties)加载键值对,以便在我的TestNG测试方法中使用它,如 @Test public void testDBConnection() { String dbEnvUsed = System.getProperty("db.env"); String keyForDatabaseDriver = System.getProperty("db.driver"); String key
@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
}
testdata.properties文件的内容
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 = ######
摘录自my pom.xml
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertiesFile>src/main/resources/testdata.properties</systemPropertiesFile>
</configuration>
</plugin>
</plugins>
maven surefire插件
src/main/resources/testdata.properties
我想在执行测试之前更改键“db.env”的值,而不必编辑testdata.properties文件。有没有办法达到同样的效果?发现maven提供了一种方法来覆盖属性文件中of键的值。下面是一种方法:
mvn -D<key.to.override>=<yourvalue>
请注意,我在testdata.properties文件中定义了一个键“db.env”,该文件由maven surefire插件加载。因此,键“db.env”的值可以通过使用System.getProperty(“db.env”)在我的代码中访问代码>
要实现这一点(加载属性文件的方式使您的键值对可以通过使用System.getProperty(“key.in.your.property”)
访问),请在pom.xml中添加以下内容
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertiesFile>src/main/resources/testdata.properties</systemPropertiesFile>
</configuration>
</plugin>
maven surefire插件
src/main/resources/testdata.properties
您是如何加载这些属性的?我很惊讶看到您在系统上查找它们,我希望有一个简单的java.util.Properties
实例,它可以让您通过waymaven surefire插件轻松保存更新的属性。如果在中指定属性文件的路径(请参阅my pom.xml的摘录),它将加载它们,您可以通过System.getProperty(“your.key”)在代码中访问它们。顺便说一句,我之前是通过创建java.util.properties的实例来访问我的属性的。我改用maven surefire插件来加载属性,因为它节省了几行代码,尽管这会影响代码的可读性(对于不熟悉此用法的人)。我不知道这个插件,但我想一旦您的属性添加到系统属性中,您无法区分此处已存在的属性和以后添加的属性。您可以始终在System.getProperties
上使用Properties.save
(也可以选择筛选系统的属性:)
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertiesFile>src/main/resources/testdata.properties</systemPropertiesFile>
</configuration>
</plugin>