문제
통합 테스트를 위해 H2 데이터베이스를 작성하고 사용하고 싶다고 가정합니다.
Maven은 테스트를 실행하라는 명령이 있습니다. mvn test
.
Maven에게 Maven에게 테스트를 위해 H2 데이터베이스 서버를 시작하고 완료되면 중지하도록 지시하는 방법이 있습니까?
Maven 명령을 통해 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 개의 작은 클래스를 만들 수 있습니다. 아이디어는 통합 테스트가 실행되기 전에 STARKERVER 클래스를 실행 한 다음 테스트가 실행 된 후 클래스 스톱서버를 실행하는 것입니다.
어딘가에 설명 된대로 DB 서버에 대해 동일하게 수행해야합니다. 이 문서 (설명은 통합 테스트에서 부두를 시작하고 중지하기위한 것입니다)
pom.xml에서 당신은 maven-exec-plugin을 정의하여 exec : Java 목표 및 2 개의 실행 (1 호출 STARKERVER의 경우 1, StopServer의 경우 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>
그것이 당신이 원하는 것이기를 바랍니다
이 플러그인은 통합 테스트 (기본 플러그인 단계) 전에 TCP 모드로 새로운 H2 DB를 스폰하는 데 적합합니다. Github의 H2-Maven-Plugin
잘 문서화되어 있지 않지만 Mojo 소스를 확인하여 구성 옵션을 알 수 있습니다. Maven Central에 게시되었습니다.
기본적으로 통합 테스트의 경우 Maven이 다음을 수행 할 수 있습니다.
- Tomcat 서버 및 H2의 경우 무작위로 사용 가능한 네트워크 포트를 예약하십시오 (포트 충돌을 피하기 위해)
- H2 서버를 시작하십시오
- Tomcat 서버를 시작하십시오
- 통합 테스트를 실행하십시오
- Tomcat 서버를 중지하십시오
- H2 서버를 중지하십시오
이것은 이와 같이 보이는 Maven 구성으로 달성 할 수 있습니다. 통합 테스트가 사용자 정의 인터페이스 주니트 카테고리로 주석이 달린다고 가정합니다.
@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 데이터 소스를 원하지 않으면 같은 속성을 사용하여 스프링 선언 데이터 소스를 사용할 수 있습니다 ...
통합 테스트 Tomcat을 설정하고 IDE에서 통합 테스트를 실행하려는 경우 추가 여행을합니다.
속성을 사용하여 Tomcat 서버를 포크하거나 포크 할 수 있습니다.
<fork>${integrationTestsForkTomcatJvm}</fork>
Fork = false를 설정하면 서버가 차단되고 Maven이 계속되지 않으므로 통합 테스트는 실행되지 않지만 IDE에서 실행할 수 있습니다.
방금 maven @ bitbucket 용 H2 플러그인 프로젝트를 시작했습니다. 나는 그것에 대한 어떤 도움에 감사드립니다.
https://bitbucket.org/dohque/maven-h2-plugin
도움이되기를 바랍니다.
내 프로젝트에서 단위 테스트를 위해 Spring 에이 데이터베이스 생성 및 초기화를 처리하도록 요청했습니다. 에 명시된 바와 같이 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>
장치 테스트를 시작할 때이 구성으로 스프링 컨텍스트를 시작하면됩니다.
단위 테스트가 실행되기 전에 파일 기반 H2 데이터베이스를 만듭니다. 파일은 target
디렉토리로 언제든지 제거 할 수 있습니다 mvn clean
.
나는 maven-sql-plugin을 다음과 같이 사용합니다.
<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-Plugin을 사용하여 시작해야합니다. 개미 작업에서 시작 및 중지 H2 엔진에 대한 코드를 작성하고 통합 테스트가 시작되고 중지 될 때 호출하십시오.
자세한 내용을 참조하십시오 http://docs.codehaus.org/display/mavenuser/maven+and+integration+testing
다음은 저를 위해 일을합니다 (단지 사용합니다 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.xml
그만큼 com.h2database:h2
프로젝트 의존성이 아니 었습니다. 당신이 그것을 가지고 있다면, 당신은 그것을 플러그인 의존성으로 명시 적으로 명시 할 필요가 없을 수 있습니다.