使用ant build,属性文件的静态初始化失败

时间:2011-08-10 11:35:07

标签: java ant testng

我有一个小的java项目,我使用TestNG + Eclipse执行,它运行良好。 我使用属性文件外部化了测试数据,并在测试类的一个静态块中初始化它 -

public class SanityTest extends SelTestCase {

static Properties properties = new Properties();
static String pickUp;
static String dropOff;

static {
    try {
        properties
                .load(SanityTest.class
                        .getResourceAsStream("/com/product/testdata/testdata.properties"));
        pickUp = properties.getProperty("pickUp");
        dropOff = properties.getProperty("dropOff");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * Verifies the booking of car on car hire market
 * @throws Exception
 */
@Test
public void testBookingModule() throws Exception {
    // Some tests here
   }

但是当我使用ant build(1.8)执行相同操作时,我在目标“run”上遇到以下异常 -

 [testng] Caused by: java.lang.NullPointerException
 [testng]   at java.util.Properties$LineReader.readLine(Properties.java:418)
 [testng]   at java.util.Properties.load0(Properties.java:337)
 [testng]   at java.util.Properties.load(Properties.java:325)

我无法弄清楚并且还检查了“bin”是否已创建且包含所有相应文件。 我错过了什么?

2 个答案:

答案 0 :(得分:1)

我觉得很奇怪你必须使用包/目录名来检索由类加载器作为资源加载的文件。

通常在使用getResourceAsStream时,将数据文件放在类所在的位置,.class文件所在的位置(或者在资源目录中,eclipse将在构建时复制它),然后通过仅指定文件名。

尝试

 properties.load(SanityTest.class.getResourceAsStream("testdata.properties"));

其中testdata.properties与SanityTest.class在同一文件夹中

此致  斯特凡

答案 1 :(得分:0)

好像你从属性文件中获取了流。 这个异常可能来自格式错误的prop文件,导致null返回某个地方吗?