繁体   English   中英

java junit 测试:从属性文件动态加载值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM