2013-03-23 74 views
2

问题是如何使用不同的属性文件进行集成测试。详细解释如下。在Spring项目中使用Maven进行集成测试

我正在尝试使用Maven使用tomcat7-maven-plugin进行容器集成测试。该项目使用Spring和JPA。目前我已经想通如下:

  • 单元测试的类名遵循的模式*测试,并与mvn testmaven-surefire-plugin
  • 集成测试的类名运行遵循的模式* IT和mvn verify通过运行maven-failsafe-plugin
  • 随着处决我可以触发启动和停止Tomcat和部署WAR文件

所有这一切工作,当我运行mvn verify在Tomcat启动。

但是,不使用常规数据库,我想使用内存数据库。我的数据库配置在通过context:property-placeholder加载的文件src/main/resources/META-INF/spring/database.properties中定义。

我试图在src/test/resources/META-INF/spring/database.properties中定义一个替代文件,但被忽略。我知道可以在tomcat7-maven-plugin中定义系统属性,但我不知道如何使用它们来触发加载不同的属性文件。

tomcat7-maven-plugin配置如下:

<plugin> 
    <groupId>org.apache.tomcat.maven</groupId> 
    <artifactId>tomcat7-maven-plugin</artifactId> 
    <version>2.1</version> 
    <configuration> 
     <port>9090</port> 
     <path>/processing</path> 
     <useTestClasspath>true</useTestClasspath> 
     <systemProperties> 
      <example.value.1>alpha</example.value.1> 
     </systemProperties> 
    </configuration> 
    <executions> 
     <execution> 
      <id>start-tomcat</id> 
      <phase>pre-integration-test</phase> 
      <goals> 
       <goal>run-war-only</goal> 
      </goals> 
      <configuration> 
       <fork>true</fork> 
      </configuration> 
     </execution> 
     <execution> 
      <id>stop-tomcat</id> 
      <phase>post-integration-test</phase> 
      <goals> 
       <goal>shutdown</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

属性由context-main.xml使用以下行加载:

<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/> 

予加载上下文配置从web.xml与下列:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:META-INF/spring/context-*.xml</param-value> 
</context-param> 

任何suggestio有关如何加载替代属性文件进行测试?

+0

'src/test/resources/META-INF/spring/database。属性“将在正常测试中被考虑进去,但在故障测试中不会被考虑,因为失败测试是基于正常的'war',并且不包括测试src或测试资源 – Ralph 2013-03-23 21:21:27

+0

对,我怀疑是这样,现在我正在试验配置文件和'maven-antrun-plugin'来直接在build文件夹中复制修改过的属性文件。如果有人有一个更好的主意,我全是耳朵。 – stivlo 2013-03-23 21:32:23

回答

2

其中一种方法是使用Maven配置文件和ant插件。我怀疑这不是最优雅的方式,我愿意倾听更好的解决方案,但现在它解决了我的问题。

配置文件是根据命令行参数具有不同Maven配置的一种方式,所以在我的情况下运行集成测试,我运行命令mvn -Pintegration clean verify。这里integration是配置文件的名称,并在pom.xml定义,在properties依次如下:

<profiles> 
    <profile> 
     <id>standard</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
    </profile> 
    <profile> 
     <id>integration</id> 
     <build> 
      <plugins> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-antrun-plugin</artifactId> 
        <version>1.1</version> 
        <executions> 
         <execution> 
          <phase>test</phase> 
          <goals> 
           <goal>run</goal> 
          </goals> 
          <configuration> 
           <tasks> 
            <echo>Copying test database.properties to ${project.build.outputDirectory}/META-INF/spring/</echo> 
            <copy file="src/test/resources/META-INF/spring/database.properties" 
             todir="${project.build.outputDirectory}/META-INF/spring/" verbose="true" overwrite="true" /> 
           </tasks> 
          </configuration> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
</profiles> 

使用mvn clean package将改为使用标准配置。有趣的是,配置文件也可以在pom之外定义.m2/settings.xml

另请参阅:Building For Different Environments with Maven 2 - 请注意,副本中的overwrite参数是必需的,在链接的文档中没有提及。