[英]java junit test: load value dynamically from properties file
我有一個 junit 測試 class 如下所示:
我希望能夠將“鍵”值存儲在應用程序屬性文件中。
所以當我運行我的測試 class 時,使用了鍵值。
我將如何將我的鍵值存儲在屬性文件中?
public class test {
static WebDriver driver;
@BeforeClass
public static void BrowserOpen() {
driver = new ChromeDriver();
}
@Test
public void test() {
int key = 12345;
}
@AfterClass
public static void BrowserClose() {
driver.quit();
}
}
假設您將以下內容放在您的資源或測試資源文件夾中的example.properties
中(該文件夾在您的構建工具中配置 - 例如您的 Maven 或 Gradle 配置或 IntelliJ 中的“模塊設置”):
key=12345
然后你可以按如下方式加載它:
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.util.Properties;
public class PropertiesExample {
private static int key;
@BeforeClass
public static void loadKey() throws IOException {
Properties properties = new Properties();
properties.load(PropertiesExample.class.getResourceAsStream("example.properties"));
key = Integer.parseInt(properties.getProperty("key"));
}
@Test
public void test() {
System.out.println(key); // prints 12345
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.