Я видел, что некоторые люди используют maven-sql-plugin, чтобы сделать это. Но это походит на задачу, которая лучше подходит для DBUnit...., возможно, в начале всего набора тестов.
Какова лучшая практика здесь?
Я использую подключаемый модуль Maven SQL
. Вам гораздо лучше использовать его и убедиться, что вы создаете и заполняете перед тестами, а затем бросаете после тестов. Вы также захотите использовать создать или заменить или drop, если существует в вашем сценарии создания (при условии, что ваша база данных поддерживает его) в случае, если тест завершится неудачно и база данных останется в какое-то противоречивое состояние.
Потребовалось немного повозиться, но мне удалось отбросить, создать и создать схему для H2 и MySQL. Для Oracle и SQL * Server 2008 все еще нужно доделать. Я вставил точные команды DROP и CREATE в свойства, а в некоторых случаях (например, H2) нужно было полностью пропустить создание базы данных. Вот как это выглядит:
<plugin>
<!-- Used to automatically drop (if any) and create a database prior to running integration test cases. -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<dependencies>
<dependency>
<!-- Adds the correct JDBC driver as a dependency of this plugin -->
<groupId>${database.groupId}</groupId>
<artifactId>${database.artifactId}</artifactId>
<version>${database.version}</version>
</dependency>
</dependencies>
<configuration>
<!-- common configuration shared by all executions -->
<driver>${database.class}</driver>
<username>${database.username}</username>
<password>${database.password}</password>
<url>${database.url}</url>
</configuration>
<executions>
<execution>
<!-- Start by dropping the database (we'll leave it intact when finished) -->
<id>drop-db</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<!-- Can't use regular URL in case database doesn't exist -->
<url>${database.url.alternate}</url>
<skip>${database.sqlDrop.skip}</skip>
<autocommit>true</autocommit>
<sqlCommand>${database.sqlDrop};</sqlCommand>
<onError>continue</onError>
</configuration>
</execution>
<execution>
<!-- then create a new database -->
<id>create-db</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<!-- Can't use regular URL in case database doesn't exist -->
<url>${database.url.alternate}</url>
<skip>${database.sqlCreate.skip}</skip>
<autocommit>true</autocommit>
<sqlCommand>${database.sqlCreate};</sqlCommand>
<onError>continue</onError>
</configuration>
</execution>
<execution>
<!-- and finally run the schema creation script we just made with the hibernate3-maven-plugin -->
<id>create-schema</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<skip>${database.sqlSchema.skip}</skip>
<autocommit>true</autocommit>
<srcFiles>
<srcFile>target/hibernate3/sql/create-${database.vendor}-schema.sql</srcFile>
</srcFiles>
<onError>continue</onError>
</configuration>
</execution>
</executions>
</plugin>