假如我想要创造和使用H2数据库为我的集成试验。

专家有一个命令,运行测试: mvn test.

有没有办法告诉家开始一个H2数据库服务器,用于测试和阻止它的时候它做了什么?

我想这一工作类似于如何,我可以运行tomcat通过一家命令(mvn tomcat:run).

对不起,如果这个问题是荒谬的,我仍然环绕我的头周围的新概念。

有帮助吗?

解决方案

我能得到它,而无需使用外部服务器的工作只是通过增加通过的Maven的依赖,以H2,然后使用这个bean:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver"/>
    <property name="url" value="jdbc:h2:file:h2\db"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>        
</bean>

然后,这种要求我在内存中使用基于文件的DB代替。但它确实特技。

其他提示

您可以创建2个小类与启动和停止数据库的主要方法。这个想法是运行startserver的类集成测试运行,然后类StopServer测试已运行前后。

你应该在这是你的数据库服务器做同样的文件(描述是用于启动和在集成测试停止码头)

在你的pom.xml应定义Maven的EXEC-插件来运行的 EXEC:JAVA 目标和创建2只执行(1用于调用用于StopServer startserver的和1):

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.1.1</version>
  <executions>
    <execution>
      <!-- start server before integration tests -->
      <id>start</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>java</goal>
      </goals>
      <configuration>
        <mainClass>com.foo.StartServer</mainClass>
      </configuration>
     </execution>
     <execution>
      <!-- stop server after integration tests -->
      <id>stop</id>
      <phase>post-integration-test</phase>
      <goals>
        <goal>java</goal>
      </goals>
      <configuration>
        <mainClass>com.foo.StopServer</mainClass>
      </configuration>
     </execution>
   </executions>
 </plugin>

希望这是你想要的东西。

这个插件的工作正产生一个新的H2DB tcp模式之前的集成试验(默认的插件阶段): h2-玛文件上。

这是不是记录良好,但你可以检查的魔力来源,以知道结构的选项。它发表在专家中心。


基本上,对于融合试验,则可能需要专家:

  • 保留随机提供网络端口,为Tomcat服务器,和你的H2(以避免冲突口)
  • 开始H2服务器
  • 开始Tomcat服务器
  • 运行一体化试验
  • 停止Tomcat服务器
  • 停止H2服务器

这是可以实现的,与一家配置这样看。假设你一体化试验annoted一个自定义的接口JUnit类别:

@Category(IntegrationTest.class)

这个专家的配置正常工作对我来说:

<profile>
   <id>it</id>
   <build>
     <plugins>

       <!-- Reserve randomly available network ports -->
       <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>build-helper-maven-plugin</artifactId>
         <executions>
           <execution>
             <id>reserve-network-port</id>
             <goals>
               <goal>reserve-network-port</goal>
             </goals>
             <phase>process-resources</phase>
             <configuration>
               <portNames>
                 <portName>tomcat.test.http.port</portName>
                 <portName>h2.test.tcp.port</portName>
               </portNames>
             </configuration>
           </execution>
         </executions>
       </plugin>


       <!-- Start H2 before integration tests, accepting tcp connections on the randomly selected port -->
       <plugin>
         <groupId>com.edugility</groupId>
         <artifactId>h2-maven-plugin</artifactId>
         <version>1.0</version>
         <configuration>
           <port>${h2.test.tcp.port}</port>
         </configuration>
         <executions>
             <execution>
               <id>Spawn a new H2 TCP server</id>
               <goals>
                 <goal>spawn</goal>
               </goals>
             </execution>
             <execution>
               <id>Stop a spawned H2 TCP server</id>
               <goals>
                 <goal>stop</goal>
               </goals>
             </execution>
           </executions>
       </plugin>


       <!-- Start Tomcat before integration tests on the -->
       <plugin>
         <groupId>org.apache.tomcat.maven</groupId>
         <artifactId>tomcat7-maven-plugin</artifactId>
         <configuration>
           <systemProperties>
             <spring.profiles.active>integration_tests</spring.profiles.active>
             <httpPort>${http.test.http.port}</httpPort>
             <h2Port>${h2.test.tcp.port}</h2Port>
           </systemProperties>
           <port>${http.test.http.port}</port>
           <contextFile>src/main/java/META-INF/tomcat/webapp-test-context-using-h2.xml</contextFile>
           <fork>true</fork>
         </configuration>
         <executions>
           <execution>
             <id>run-tomcat</id>
             <phase>pre-integration-test</phase>
             <goals>
               <goal>run</goal>
             </goals>
           </execution>
           <execution>
             <id>stop-tomcat</id>
             <phase>post-integration-test</phase>
             <goals>
               <goal>shutdown</goal>
             </goals>
           </execution>
         </executions>
         <dependencies>
           <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>${mysql.version}</version>
           </dependency>
           <dependency>
             <groupId>com.h2database</groupId>
             <artifactId>h2</artifactId>
             <version>${h2.version}</version>
           </dependency>
         </dependencies>
       </plugin>


       <!-- Run the integration tests annotated with @Category(IntegrationTest.class) -->
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-failsafe-plugin</artifactId>
         <!-- Bug in 2.12.x -->
         <version>2.11</version>
         <dependencies>
           <dependency>
             <groupId>org.apache.maven.surefire</groupId>
             <artifactId>surefire-junit47</artifactId>
             <version>2.12.4</version>
           </dependency>
         </dependencies>
         <configuration>
           <groups>com.mycompany.junit.IntegrationTest</groups>
           <failIfNoTests>false</failIfNoTests>
           <junitArtifactName>junit:junit-dep</junitArtifactName>
           <systemPropertyVariables>
             <httpPort>${tomcat.test.http.port}</httpPort>
             <h2Port>${h2.test.tcp.port}</h2Port>
           </systemPropertyVariables>
         </configuration>
         <executions>
           <execution>
             <goals>
               <goal>integration-test</goal>
             </goals>
           </execution>
         </executions>
       </plugin>

     </plugins>
   </build>
 </profile>

你可能需要使用专家的过滤器在tomcat背景文件,以便在港口改为:

   <contextFile>src/main/java/META-INF/tomcat/webapp-test-context-using-h2.xml</contextFile>

与文件的内容:

  <Resource name="jdbc/dataSource"
            auth="Container"
            type="javax.sql.DataSource"
            maxActive="100"
            maxIdle="30"
            maxWait="10000"
            username=""
            password=""
            driverClassName="org.h2.Driver"
            url="jdbc:h2:tcp://localhost:${h2.test.tcp.port}/mem:db;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL"/>

或如果你不想JNDI数据源,可使用弹簧声明的数据源,使用相同的财产...


一个额外的旅行,如果你想要能够设置你的集成试验tomcat,并运行一体化测试你的IDE:

你可以使用一个财叉或不Tomcat服务器:

<fork>${integrationTestsForkTomcatJvm}</fork>

当你设置叉=false、服务器将块和家不会继续,这样的集成试验将不会跑,但是你将能够运行他们自己的环境。

我刚刚开始项目H2插件行家@到位桶。我将理解任何与它的帮助。

https://bitbucket.org/dohque/maven-h2-plugin

希望这将是有帮助的。

在我的项目,单元测试,我问春天来处理该数据库的创建和初始化。正如 H2文档说,你可以创建一个豆:

<bean id = "org.h2.tools.Server"
    class="org.h2.tools.Server"
    factory-method="createTcpServer"
    init-method="start"
    destroy-method="stop">
    <constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,8043" />
</bean>

您只需要启动这个配置Spring上下文当你开始你的单元测试。

创建基于文件的H2数据库单元测试运行之前。该文件住在target目录,并可以在任何时候使用mvn clean被删除。

我使用maven-SQL插件如下:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>sql-maven-plugin</artifactId>
  <version>1.5</version>
  <dependencies>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId> 
      <version>1.3.166</version>
    </dependency>
  </dependencies>
  <configuration>
    <driver>org.h2.Driver</driver>
    <url>jdbc:h2:file:target/db/testdb</url>
    <username>sa</username>
    <password></password>
    <autocommit>true</autocommit>
    <skip>${maven.test.skip}</skip>
  </configuration>
  <executions>
    <execution>
      <id>create-db</id>
      <phase>process-test-resources</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <srcFiles>
          <srcFile>${sql.dir}/drop_db.sql</srcFile>
          <srcFile>${sql.dir}/tables.sql</srcFile>
          <srcFile>${sql.dir}/constraints.sql</srcFile>
          ... etc ...
        </srcFiles>
      </configuration>
    </execution>
  </executions>
</plugin>

在数据库可以通过运行mvn process-test-resources创建。进行测试时,请确保您在target/db/testdb通过Hibernate属性连接到数据库。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
      destroy-method="close"
      p:driverClassName="org.h2.Driver"
      p:url="jdbc:h2:file:target/db/testdb"
      p:username="sa"
      p:password="" />

您还需要在Maven的依赖上com.h2database.h2的依赖关系。

如果你想在内存中,然后只需用一个不同的URL:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver"/>
    <property name="url" value="jdbc:h2:mem:db"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>        
</bean>

可以得到另外的选项,如:; DB_CLOSE_DELAY = -1

请参阅: http://www.h2database.com/html/features.html# in_memory_databases

由于H2不能提供Maven插件,你应该使用maven-antrun-插件启动它。编写代码的开始和Ant任务停止H2引擎,并调用它,当你的集成测试启动和停止。

请参阅上 http://docs.codehaus.org/细节显示/ MAVENUSER / Maven的+和+集成+测试

以下确实为我(只是使用h2依赖性和exec-maven-plugin)作业:

    <build>
        <plugins>
            <!-- start/stop H2 DB as a server -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>start-h2</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>org.h2.tools.Server</mainClass>
                            <arguments>
                                <argument>-tcp</argument>
                                <argument>-tcpDaemon</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-h2</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>org.h2.tools.Server</mainClass>
                            <arguments>
                                <argument>-tcpShutdown</argument>
                                <argument>tcp://localhost:9092</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <includeProjectDependencies>true</includeProjectDependencies>
                    <includePluginDependencies>true</includePluginDependencies>
                    <executableDependency>
                        <groupId>com.h2database</groupId>
                        <artifactId>h2</artifactId>
                    </executableDependency>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.h2database</groupId>
                        <artifactId>h2</artifactId>
                        <version>1.3.173</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
</build>

请注意,在我pom.xmlcom.h2database:h2没有项目依赖。 如果你拥有它,你可能并不需要明确它的名字作为插件依赖性。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top