java - Junit 替换属性

标签 java junit

我想更改应该从哪个 .properties 文件类获取它们。
我的类(class)现在是这样的:

public class MyClass {

private String str;    

public MyClass() throws IOException {
    loadProperties();
}

private void loadProperties() throws IOException {
    Properties props = new Properties();
    props.load(getClass().getClassLoader().getResourceAsStream("my.properties"));

    str= props.getProperty("property");        
}

为什么测试我想从另一个文件加载属性。
这是 apache Camel 应用程序,所以我现在有了这个:

public class ConverterTest {
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new MyClass(); //--> Here i must load from another file             
    }

    @Test
    // test    
}

这能实现吗?

最佳答案

只需将属性文件名传递给 MyClass 构造函数

public MyClass(String propsFile) throws IOException {
    loadProperties(propsFile);
}

关于java - Junit 替换属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19725552/

相关文章:

java - 如何使用 Java 1.7 编写 Junit,同时使用 1.6 运行基本代码

unit-testing - 通过模拟 getter 测试类

Java Jackson 如何在自定义序列化程序中为对象使用默认序列化程序

java - Truth.assertAbout 和 JavaSourceSubjectFactory.javaSource()

java - IntelliJ 在鼠标悬停时显示 JavaDocs 工具提示

Java 8 使用流来区分具有重复字段值的对象

java - 什么是 "FLF",[03]

java - 读取 EOF 后使用 Scanner 类读取 Standard.In 时出现 NoSuchElementException

java - Mockito ArgumentCapture 无法识别多个泛型类型

java - JUnit 5 在 Debug模式下截断堆栈跟踪并使 JVM 崩溃