gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:48:25 24 4
gpt4 key购买 nike

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

@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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com