java - 参数化 JUnit : Loading multiple property files from String array

标签 java properties junit parameterized-unit-test

我正在尝试编写一个从字符串数组加载不同属性文件的测试。但代码不断抛出空指针异常,请问有什么想法吗?

@RunWith(value = Parameterized.class)
public class AllTests 
{ 
     private static String text;
     private static Properties props;

     public AllTests(String text) 
     {
        AllTests.text= text;
     }

     @Parameters
     public static List<String[]> data() 
     {
       String[][] data = new String[][] { { "test.properties" }};
    return Arrays.asList(data);
     }

         @BeforeClass
     public static void setup()
     {  
           props = new Properties();
         try 
         {
            //load a properties file 
        props.load(new FileInputStream(text));
         } 
         catch (IOException ex) 
         {
        ex.printStackTrace();
         }
 }

 @Test
 public void test() 
 {
        System.out.println(text);
 }}

我做了一些进一步的调查,发现 @Test stub 可以工作,但 @BeforeClass 返回 null,我可以不使用设置中的参数吗?

@RunWith(value = Parameterized.class)
public class AllTests 
{ 
     private static String client;

public AllTests(String client) { AllTests.client = client; } @Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { "oxfam.properties" }}; return Arrays.asList(data); } @BeforeClass public static void setup() { System.out.println(client); } @Test public void test() { System.out.println(client); }}

最佳答案

props 类变量从未初始化。尝试在声明时初始化它:

private static Properties props = new Properties();

关于java - 参数化 JUnit : Loading multiple property files from String array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16695145/

相关文章:

java - Android 库中的 Android NoClassDefFound

javascript - .checked=true 不使用 jquery $ 函数

junit - JUnit 测试。如何?

spring - Spring中的JUnit测试-覆盖和忽略来自应用程序其他配置类的bean

java - 更改 Swing 的外观

java - JPA CriteriaQuery 比较时间戳忽略时间部分

java - 从 Java 到 Ajax 的错误消息未正确返回

properties - 闭包的计算属性

python - 在 Python 中是否有等同于 @property 的容器?

java - 在类路径 :/cucumberOptions 中找不到任何功能