这似乎是一个如此愚蠢的问题,但我在网上找到的解决方案并没有为我效劳。我正在Maven项目中编写集成测试,我需要从属性文件中读取值,我已将其放入src/test/resources
。
我的测试尝试在构建期间读取此属性文件:
public ControllerIT() throws Exception {
wc = new WebConversation();
prop = new Properties();
InputStream stream = getClass().getResourceAsStream( "/test.properties" );
prop.load( stream );
}
当我运行测试时,我总是在NullPointerException
的通话中获得prop.load( stream );
。
我已经尝试过我在网上找到的所有解决方案:
test.properties
而不是/test.properties
getClass().getClassLoader().getResourceAsStream( "test.properties" );
但没有任何作用。
我认为,对于额外的分数,理想的解决方案将允许我通过CLI上的mvn integration-test
以及Eclipse中的Run As
- >运行此测试。 JUnit test
。为此,我想我还应该提一下,我已经明确地将main/test/resources
添加为Eclipse中的源文件夹,但它仍未正确加载文件。
根据要求,这是我的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>
如您所见,这是一个独立的集成测试套件(它基本上只是将POST请求发送到Web服务)。我用mvn verify
运行它并看到:
$ 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] ------------------------------------------------------------------------
在此输出中,ControllerIT
的第30行是对prop.load( inputstream )
的调用。
答案 0 :(得分:2)
您的项目是包装类型pom。这样,资源插件不包含在生命周期中(并且没有资源会被复制到目标并因此落在类路径中)
将您的包装更改为jar
,一切都应该有效。