2013-10-16 25 views
1

我希望能夠在Java測試最近使用的某些屬性文件中配置值。 這裏是我的pom.xml用maven過濾的propery文件運行測試

<profiles> 
     <profile> 
      <id>env-dev</id> 
      <activation> 
       <property> 
        <name>env</name> 
        <value>dev</value> 
       </property> 
      </activation> 
      <properties> 
       <target.env>http://myurl.com</target.env> 
      </properties> 
     </profile> 
    </profiles> 
    <build> 
     <resources> 
      <resource> 
       <directory>src/test/java</directory> 
       <filtering>true</filtering> 
      </resource> 
     </resources> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <configuration> 
        <suiteXmlFiles> 
         <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> 
        </suiteXmlFiles> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

屬性文件

targetEnv=${target.env} 

當我運行mvn install -Denv=dev我可以看到目標文件夾中,該屬性文件獲得正確的值,但在測試過程中的佔位符在測試運行時使用我想要使用真實值的地方。

我在做什麼錯?

感謝

回答

0

我在做什麼錯?

首先,不要使用配置文件在環境之間切換。尤其是單元測試不應該意識到任何環境設置。請在這種情況下刪除它。 爲了過濾測試資源,您應該使用<testResources/> -tag代替。