Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ant build静态初始化属性文件失败_Java_Ant_Testng - Fatal编程技术网

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

Java 使用ant build静态初始化属性文件失败,java,ant,testng,Java,Ant,Testng,我有一个小java项目,我使用TestNG+Eclipse执行它,它运行得很好。 我使用属性文件将测试数据外部化,并在测试类的一个静态块中初始化它- public class SanityTest extends SelTestCase { static Properties properties = new Properties(); static String pickUp; static String dropOff; static { try { proper

我有一个小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”上遇到以下异常-

我想不出多少,还检查了“bin”是否已创建,是否有所有相应的文件。
我遗漏了什么吗?

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

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

试一试

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

问候,,
stéphane

您似乎从属性文件中获得了流。
这个异常可能来自格式不正确的prop文件,这会导致在某个地方返回null吗?

哦,我想将我的数据从类中分离出来,这就是为什么我将它保存在不同的目录中。否则,我的类和测试数据都将放在一个包中。您是否建议使用其他方法将数据文件保存在与类相同的包中?您可以使用eclipse。项目的典型maven原型将允许您在单独的文件夹中拥有测试资源,在单独的文件夹中拥有普通资源,在一个文件夹中拥有普通应用程序类,在另一个文件夹中拥有测试类。您是如何格式化prop文件的内容的?在这种情况下,当我尝试使用TestNG执行测试时,应该会遇到相同的异常,但我没有。我只有在使用ant时才会遇到异常!好的,我得到了答案,因为属性文件不是java类,所以我必须做一个复制任务,以便它们可以在构建文件夹中使用-
 [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)
 properties.load(SanityTest.class.getResourceAsStream("testdata.properties"));