質問

だいたいの作成と使用のH2データベースのための私の統合試験までを実施。

Mavenはコマンドを実行試験: mvn test.

はしてもらえると助かりますかmavenを開始H2データベースサーバーのための試験を止めるつもりです。

私は、この作業に似ていきtomcatによMavenコマンド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後の試験をしています。

にするべきなのと同じごDBサーバとして記のどこかで この文書 (説明するための開始と停止および桟橋に統合試験)

おpom.xml を定義する必要がありますmaven-exec-プラグインの exec:java 目標を2死刑を執(1呼StartServer、1StopServer):

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

こうしたい

このプラグインは、統合テスト (デフォルトのプラグイン フェーズ) の前に、tcp モードで新しい H2 DB を生成するために正常に機能します。 github の h2-maven-plugin

十分に文書化されていませんが、Mojo ソースをチェックして構成オプションを知ることができます。Maven Central で公開されています。


基本的に、統合テストの場合、Maven で次のことを行うことができます。

  • Tomcat サーバーと H2 用に、ランダムに利用可能なネットワーク ポートを予約します (ポートの競合を避けるため)。
  • H2サーバーを起動します
  • Tomcatサーバーを起動します
  • 統合テストを実行する
  • Tomcatサーバーを停止します
  • H2サーバーを停止します

これは、次のような Maven 構成で実現できます。統合テストにカスタム インターフェイス JUnit カテゴリの注釈が付けられていると仮定します。

@Category(IntegrationTest.class)

この Maven 構成は私にとってはうまく機能します。

<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 コンテキスト ファイルで Maven フィルターを使用して、ポートを置き換えることができます。

   <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 データソースが必要ない場合は、同じプロパティを使用して Spring で宣言された dataSource を使用できます。


統合テスト Tomcat をセットアップし、IDE から統合テストを実行できるようにするには、さらに 1 回の作業が必要です。

プロパティを使用して、Tomcat サーバーをフォークするかどうかを指定できます。

<fork>${integrationTestsForkTomcatJvm}</fork>

fork=false を設定すると、サーバーがブロックされ、Maven が続行されなくなるため、統合テストは実行されませんが、IDE から実行できるようになります。

私はMavenの@のビットバケット用H2プラグインのプロジェクトを開始しました。私はそれを持つ任意の助けに感謝します。

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

は、それが役に立つことを願っています。

私のプロジェクトでは、ユニットテストのために、私はこのデータベースの作成と初期化を処理するために春を尋ねました。 H2ドキュメントの中で述べたように、あなたはそのためのBeanを作成することができます

<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にデータベースへの接続を確認してください。

<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