Java JUnit 是否支持用于测试的属性文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1557562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:06:15  来源:igfitidea点击:

Does JUnit support properties files for tests?

javajunit

提问by Dave Jensen

I have JUnit tests that need to run in various different staging environments. Each of the environments have different login credentials or other aspects that are specific to that environment. My plan is to pass an environment variable into the VM to indicate which environment to use. Then use that var to read from a properties file.

我有需要在各种不同的登台环境中运行的 JUnit 测试。每个环境都有不同的登录凭据或特定于该环境的其他方面。我的计划是将环境变量传递给 VM 以指示要使用的环境。然后使用该 var 从属性文件中读取。

Does JUnit have any build in capabilities to read a .properties file?

JUnit 是否有任何内置功能来读取 .properties 文件?

采纳答案by pstanton

java has built in capabilities to read a .properties file and JUnit has built in capabilities to run setup code before executing a test suite.

java 内置了读取 .properties 文件的功能,而 JUnit 内置了在执行测试套件之前运行设置代码的功能。

java reading properties:

java读取属性:

Properties p = new Properties();
p.load(new FileReader(new File("config.properties")));

junit startup documentation

junit 启动文档

put those 2 together and you should have what you need.

把这两个放在一起,你应该有你需要的。

回答by Nate

Can't you just read the properties file in your setup method?

你不能在你的设置方法中读取属性文件吗?

回答by edvox1138

//
// Load properties to control unit test behaviour.
// Add code in setUp() method or any @Before method (JUnit4).
//
// Corrected previous example: - Properties.load() takes an InputStream type.
//
import java.io.File;
import java.io.FileInputStream;        
import java.util.Properties;

Properties p = new Properties();
p.load(new FileInputStream( new File("unittest.properties")));

// loading properties in XML format        
Properties pXML = new Properties();
pXML.loadFromXML(new FileInputStream( new File("unittest.xml")));

回答by scott m gardner

It is usually preferred to use class path relative files for unit test properties, so they can run without worrying about file paths. The path may be different on your dev box, or the build server, or where ever. This will also work from ant, maven, eclipse without changes.

通常首选使用类路径相关文件作为单元测试属性,这样它们就可以运行而无需担心文件路径。您的开发箱、构建服务器或其他地方的路径可能不同。这也适用于 ant、maven、eclipse,无需更改。

private Properties props = new Properties();

InputStream is = ClassLoader.getSystemResourceAsStream("unittest.properties");
try {
  props.load(is);
}
catch (IOException e) {
 // Handle exception here
}

putting the "unittest.properties" file at the root of the classpath.

将“unittest.properties”文件放在类路径的根目录下。

回答by Barett

This answer is intended to help those who use Maven.

这个答案旨在帮助那些使用 Maven 的人。

I also prefer to use the local classloader and close my resources.

我也更喜欢使用本地类加载器并关闭我的资源。

  1. Create your test properties file, called /project/src/test/resources/your.properties

  2. If you use an IDE, you may need to mark /src/test/resources as a "Test Resources root"

  3. add some code:

  1. 创建名为 /project/src/test/resources/your.properties 的测试属性文件

  2. 如果您使用 IDE,您可能需要将 /src/test/resources 标记为“测试资源根”

  3. 添加一些代码:



// inside a YourTestClass test method

try (InputStream is = loadFile("your.properties")) {
    p.load(new InputStreamReader(is));
}

// a helper method; you can put this in a utility class if you use it often

// utility to expose file resource
private static InputStream loadFile(String path) {
    return YourTestClass.class.getClassLoader().getResourceAsStream(path);
}