Java 参数化JUnit:从字符串数组加载多个属性文件
我正在尝试编写一个测试,从字符串数组加载不同的属性文件。但是代码一直抛出空指针异常,有什么想法吗Java 参数化JUnit:从字符串数组加载多个属性文件,java,properties,junit,parameterized-unit-test,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=
@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);
}}
@RunWith(值=参数化的.class)
公共类所有测试
{
私有静态字符串文本;
私有静态属性道具;
公共所有测试(字符串文本)
{
AllTests.text=文本;
}
@参数
公共静态列表数据()
{
字符串[][]数据=新字符串[][{{{“test.properties”};
返回数组.asList(数据);
}
@课前
公共静态无效设置()
{
props=新属性();
尝试
{
//加载属性文件
加载(新文件输入流(文本));
}
捕获(IOEX异常)
{
例如printStackTrace();
}
}
@试验
公开无效测试()
{
System.out.println(文本);
}}
我做了进一步的调查,发现@teststub可以工作,但是@BeforeClass返回null,我可以不使用设置中的参数吗
@RunWith(值=参数化的.class)
公共类所有测试
{
私有静态字符串客户端
public所有测试(字符串客户端)
{
AllTests.client=客户端;
}
@参数
公共静态收集数据()
{
对象[][]数据=新对象[][{{“oxfam.properties”};
返回数组.asList(数据);
}
@课前
公共静态无效设置()
{
System.out.println(客户端);
}
@试验
公开无效测试()
{
System.out.println(客户端);
}}
从未初始化
props
类变量。尝试在声明时初始化它:
private static Properties props = new Properties();
props
类变量从未初始化。尝试在声明时初始化它:
private static Properties props = new Properties();
正如布伦特所说,最初的错误是因为道具没有初始化。但是,您的测试不起作用的原因是因为您使用的是静态字段。它们应该是实例字段,只有
data()
应该是静态的
以下工作:
@RunWith(value = Parameterized.class)
public class AllTests {
private String text;
private Properties props;
public AllTests(String text) {
this.text = text;
props = new Properties();
try {
// load a properties file
props.load(new FileInputStream(text));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Parameters
public static List<String[]> data() {
String[][] data = new String[][] { { "test.properties" } };
return Arrays.asList(data);
}
@Test
public void test() {
System.out.println(text);
}
}
@RunWith(值=参数化的.class)
公共类所有测试{
私有字符串文本;
私人财产道具;
公共所有测试(字符串文本){
this.text=文本;
props=新属性();
试一试{
//加载属性文件
加载(新文件输入流(文本));
}捕获(IOEX异常){
例如printStackTrace();
}
}
@参数
公共静态列表数据(){
字符串[][]数据=新字符串[][{{{“test.properties”};
返回数组.asList(数据);
}
@试验
公开无效测试(){
System.out.println(文本);
}
}
JUnit调用data()
方法,然后为data()
方法返回的每个值创建AllTests
类的实例,构造函数参数也来自data()
方法。因此,文本和道具字段应该是实例字段
因此,在您的示例中,在构造函数之前调用了
@BeforeClass
,因此出现了空指针异常。正如Brent所说,原始错误是因为未初始化道具。但是,您的测试不起作用的原因是因为您使用的是静态字段。它们应该是实例字段,只有data()
应该是静态的
以下工作:
@RunWith(value = Parameterized.class)
public class AllTests {
private String text;
private Properties props;
public AllTests(String text) {
this.text = text;
props = new Properties();
try {
// load a properties file
props.load(new FileInputStream(text));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Parameters
public static List<String[]> data() {
String[][] data = new String[][] { { "test.properties" } };
return Arrays.asList(data);
}
@Test
public void test() {
System.out.println(text);
}
}
@RunWith(值=参数化的.class)
公共类所有测试{
私有字符串文本;
私人财产道具;
公共所有测试(字符串文本){
this.text=文本;
props=新属性();
试一试{
//加载属性文件
加载(新文件输入流(文本));
}捕获(IOEX异常){
例如printStackTrace();
}
}
@参数
公共静态列表数据(){
字符串[][]数据=新字符串[][{{{“test.properties”};
返回数组.asList(数据);
}
@试验
公开无效测试(){
System.out.println(文本);
}
}
JUnit调用data()
方法,然后为data()
方法返回的每个值创建AllTests
类的实例,构造函数参数也来自data()
方法。因此,文本和道具字段应该是实例字段
因此,在您的示例中,在构造函数之前调用了
@BeforeClass
,因此出现了空指针异常;这是抛出null pointerprops.load(新文件输入流(客户端))的行;这是抛出null的行pointer@DavidCunningham然后文本为空。@DavidCunningham然后文本为空。