简体   繁体   English

加载Maven-surefire-plugin的属性文件

[英]Loading property file for maven-surefire-plugin

I have a testng class which tests a class which in turns load property from classpath.When i run maven install i am getting an error mentioning file not found.It is not able to find the property file though i have it in src/test/resources . 我有一个testng类,该类测试一个类,该类依次从classpath加载属性。当我运行maven install时,我收到一个错误提示文件,找不到文件。尽管我在src/test/resources有它,但它找不到属性文件src/test/resources How to resolve this issue? 如何解决这个问题?

the java code is having a constructor which tries to load the property file Properties props = JobUtils .loadProperties("x.properties"); Java代码具有一个试图加载属性文件的构造函数。 Properties props = JobUtils .loadProperties("x.properties"); my maven configuration: 我的Maven配置:


<plugin>
  <groupId>org.apache.maven.plugins
  <artifactId>maven-surefire-plugin
  <version>2.5
  <configuration>
    <skipTests>false</skipTests>
  </configuration>
</plugin>

with: 与:


<build>
  <testResources>
    <testResource>
      <directory>src/test/resources</directory>
      <filtering>false</filtering>
    </testResource>
  </testResources>
</build>

Also I have this x.properties in src/test/resource 我在src/test/resource也有这个x.properties

Stack trace Running TestSuite is: 运行TestSuite的堆栈跟踪为:


java.io.FileNotFoundException: x.properties (The system cannot find the file specified)
   at java.io.FileInputStream.open(Native Method)
   at java.io.FileInputStream.(FileInputStream.java:106)
   at java.io.FileInputStream.(FileInputStream.java:66)

Also note the test class i have tries to instantiate this java code and test it. 还要注意我尝试实例化此Java代码并对其进行测试的测试类。

Your x.properties file from the src/test/resources directory is actually being copied to the target/test-classes directory after the Maven process-test-resources phase is executed. 在执行Maven process-test-resources阶段之后,实际上x.properties src/test/resources目录中的x.properties文件复制到target/test-classes目录中。 So if you want your application to be able to find it, you should use the following file-path: 因此,如果希望您的应用程序能够找到它,则应使用以下文件路径:


Properties props = JobUtils .loadProperties("target/test-classes/x.properties");

However I do not think this is exactly what you are looking for. 但是我不认为这正是您要寻找的。 I recommend to read the property file directly from the CLASSPATH (without using the file-system directly) using a code like this: 我建议使用如下代码直接从CLASSPATH中读取属性文件(不直接使用文件系统):


InputStream is = getClass().getResourceAsStream("/x.properties");
Properties p = new Properties();
try {
  p.load(is);
} catch (IOException e) {
  e.printStackTrace();
} finally {
  if (is != null) {
    try {
      is.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Using this approach you will be able to load the property file even from within a JAR file distributed to the customer or also from a configuration file that is added to the CLASSPATH in some kind of a launch-script. 使用这种方法,您甚至可以从分发给客户的JAR文件中,或者从以某种启动脚本添加到CLASSPATH中的配置文件中加载属性文件。

I had the same problem of a file not being found. 我遇到了找不到文件的相同问题。 However, I got to a point where I realized that maven found it for use when I ran mvn test on the command line. 但是,当我在命令行上运行mvn test时,我意识到maven可以使用它。 It was just my Eclipse IDE telling me it can't find it. 只是我的Eclipse IDE告诉我找不到它。 When I changed in my IDE directory src/test/resources into a "source directory", then my IDE was fine too. 当我在IDE目录src / test / resources中将其更改为“源目录”时,我的IDE也很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM