Question

This seems like such a silly question but no solutions I've found online have worked for me. I'm writing an integration test in a Maven project, and I need to read values from a properties file, which I've put in src/test/resources.

My test tries to read this properties file during construction:

public ControllerIT() throws Exception {
  wc = new WebConversation();
  prop = new Properties();
  InputStream stream = getClass().getResourceAsStream( "/test.properties" );
  prop.load( stream );
}

When I run the test, I always get a NullPointerException on the call to prop.load( stream );.

I've tried just about every permutation of solutions that I've found online:

  • Referencing the file as test.properties instead of /test.properties
  • Getting the input stream via getClass().getClassLoader().getResourceAsStream( "test.properties" );

But nothing works.

And I guess for extra points, the ideal solution would allow me to run this test via mvn integration-test on the CLI as well as in Eclipse via Run As -> JUnit test. To that end, I guess I should also mention that I've explicitly added main/test/resources as a source folder in Eclipse, but it's still not loading the file correctly.

And as requested, here is my 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>

As you can see, this is a stand-alone integration test suite (it basically just sends POST requests to a web service). I run it with mvn verify and see:

$ 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] ------------------------------------------------------------------------

In this output, line 30 of ControllerIT is the call to prop.load( inputstream ).

Était-ce utile?

La solution

Your project is of packaging type pom. That way, resources-plugin is not included in lifecycle (and no resources will be copied over to target and thus land in the classpath)

Change your packaging to jar, and everything should work.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top