Eclipse 在Maven测试中读取属性文件
这似乎是一个愚蠢的问题,但我在网上找到的任何解决方案都不适合我。我正在一个Maven项目中编写一个集成测试,需要从属性文件中读取值,我把它放在Eclipse 在Maven测试中读取属性文件,eclipse,maven,integration-testing,Eclipse,Maven,Integration Testing,这似乎是一个愚蠢的问题,但我在网上找到的任何解决方案都不适合我。我正在一个Maven项目中编写一个集成测试,需要从属性文件中读取值,我把它放在src/test/resources中 我的测试在构造期间尝试读取此属性文件: public ControllerIT() throws Exception { wc = new WebConversation(); prop = new Properties(); InputStream stream = getClass().getReso
src/test/resources
中
我的测试在构造期间尝试读取此属性文件:
public ControllerIT() throws Exception {
wc = new WebConversation();
prop = new Properties();
InputStream stream = getClass().getResourceAsStream( "/test.properties" );
prop.load( stream );
}
当我运行测试时,我总是在调用prop.load(stream)时得到NullPointerException
代码>
我已经尝试了我在网上找到的各种解决方案:
- 将文件引用为
test.properties
而不是/test.properties
- 通过
getClass().getClassLoader().getResourceAsStream(“test.properties”)获取输入流代码>
但什么都不管用
另外,我想理想的解决方案将允许我通过CLI上的mvn集成测试运行此测试,以及在Eclipse中通过runas
->JUnit测试运行此测试。为此,我想我还应该提到,我已经在Eclipse中显式地添加了main/test/resources
作为源文件夹,但它仍然没有正确加载文件
根据要求,这是我的POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>listener-testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>listener-testing</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<!-- Failsafe configuration for running integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<!-- Surefire configuration for generating reports -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.16</version>
<reportSets>
<reportSet>
<id>integration-tests</id>
<reports>
<report>failsafe-report-only </report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>httpunit</groupId>
<artifactId>httpunit</artifactId>
<version>1.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.x</version>
</dependency>
</dependencies>
</project>
在这个输出中,ControllerIT
的第30行是对prop.load(inputstream)
的调用,您的项目是打包类型pom。这样,资源插件就不包括在生命周期中(并且不会将任何资源复制到目标,从而进入类路径)
将您的打包更改为jar
,一切都应该正常。请显示完整的pom文件和toue属性文件的位置。如何开始集成测试?您是否使用mvn verify
?添加了POM和maven输出。似乎与此相关:[INFO]无法在存储库libs版本中找到资源“junit:junit:POM:debian”(http://artifactory:8081/artifactory/libs-发行版)下载:http://artifactory:8081/artifactory/simple/vm/junit/junit/debian/junit-debian.pom
?哈!当我第一次遇到这个问题时,我最初使用的是jar
的包装。我将其更改为pom
,因为我不希望它尝试运行单元测试(因为没有单元测试)。看起来我没有使用带有类加载器和/或属性文件路径正确组合的jar
包。
$ mvn verify
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building listener-testing
[INFO] task-segment: [verify]
[INFO] ------------------------------------------------------------------------
[INFO] [site:attach-descriptor {execution: default-attach-descriptor}]
Downloading: http://artifactory:8081/artifactory/simple/vm/junit/junit/debian/junit- debian.pom
[INFO] Unable to find resource 'junit:junit:pom:debian' in repository libs-release (http://artifactory:8081/artifactory/libs-release)
Downloading: http://artifactory:8081/artifactory/simple/vm/junit/junit/debian/junit-debian.pom
[INFO] Unable to find resource 'junit:junit:pom:debian' in repository central (http://repo1.maven.org/maven2)
[INFO] [failsafe:integration-test {execution: default}]
[INFO] Failsafe report directory: /home/jmp/desktop/listener-testing/target/failsafe-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.integration.ControllerIT
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.156 sec <<< FAILURE! - in com.example.integration.ControllerIT
testRequest(com.example.integration.ControllerIT) Time elapsed: 0.006 sec <<< ERROR!
java.lang.NullPointerException: null
at java.util.Properties$LineReader.readLine(Properties.java:435)
at java.util.Properties.load0(Properties.java:354)
at java.util.Properties.load(Properties.java:342)
at com.example.integration.ControllerIT.<init> (ControllerIT.java:30)
Results :
Tests in error:
ControllerIT.<init>:30 » NullPointer
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] [failsafe:verify {execution: default}]
[INFO] Failsafe report directory: /home/jmp/desktop/listener-testing/target/failsafe-reports
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] There are test failures.
Please refer to /home/jmp/desktop/listener-testing/target/failsafe-reports for the individual test results.
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Fri Mar 14 10:13:04 GMT 2014
[INFO] Final Memory: 12M/125M
[INFO] ------------------------------------------------------------------------