2014-03-14 25 views
1

這似乎是一個如此愚蠢的問題,但我在網上找到的解決方案並沒有解決我的問題。我在Maven項目中編寫了一個集成測試,我需要從屬性文件中讀取值,我已經把它放在了src/test/resources在Maven測試中讀取屬性文件

我的測試嘗試讀取施工期間這個屬性文件:

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

當我運行測試,我總是在電話會議中prop.load(stream);得到了NullPointerException。通過getClass().getClassLoader().getResourceAsStream("test.properties");

  • 引用該文件作爲test.properties而不是/test.properties
  • 獲取輸入流:

    我已經嘗試了關於解決方案的每一個排列,我已經在網上找到但沒有任何工作。

    我想加分,理想的解決方案將允許我通過CLI上的mvn integration-test以及通過Run As - >JUnit test在Eclipse中運行此測試。爲此,我想我還應該提及,我已經明確將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

請顯示您的完整pom文件和toue屬性文件的位置。你如何開始集成測試?你使用'mvn verify'? – khmarbaise

+0

增加了POM和maven輸出。 – jpappe

+0

似乎與此有關:'[INFO]無法找到資源'junit:junit:pom:debian'in repository libs-release(http:// artifactory:8081/artifactory/libs-release) 正在下載:http :// artifactory:8081/artifactory/simple/vm/junit/junit/debian/junit-debian.pom'? – khmarbaise

回答

2

您的項目是包裝類型的pom。這樣一來,資源,插件不包含在生命週期中(沒有資源將被複制到目標,從而在classpath地)

您的包裝更改爲jar,一切都應該工作。

+0

哈!當我第一次遇到問題時,我最初使用了一個'jar'的包裝。我將它改爲'pom',因爲我不想它嘗試運行單元測試(因爲沒有)。它看起來像我沒有使用正確的ClassLoader組合和/或路徑到屬性文件的'jar'包。 – jpappe