我具有以下项目结构:
project
|- src/main/resources
|- persistence.xml
|- src/test/resources
|- persistence.xml
生产系统正在使用第一个persistence.xml中定义的Postgres持久性单元。集成测试使用第二个persistence.xml中定义的H2持久性单元。
生产性persistence.xml:
<persistence-unit name="default" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ProdDS</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
测试persistence.xml:
<persistence-unit name="test-local" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>domain.user.User</class>
<class>domain.user.UserFollowerFollowingRelationship</class>
<class>domain.user.UserRoleRelationship</class>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
<property name="hibernate.connection.user" value="sa" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
我的集成测试使用DBUnit进行数据库状态测试:
public class TestResourceIT {
private final String baseURI = IntegrationTestUtil.getBaseURI();
@Rule
public EntityManagerProvider entityManagerProvider =
EntityManagerProvider.instance("test-local");
@Rule
public DBUnitRule dbUnitRule = DBUnitRule.instance(() ->
entityManagerProvider.connection());
@Test
@DataSet(strategy = SeedStrategy.CLEAN_INSERT, cleanBefore = true,
transactional = true, disableConstraints = true)
@ExpectedDataSet(value = "datasets/test-create-expected.yml")
public void createShouldReturn201() {
JsonObject testObject = Json.createObjectBuilder()
.add("message", "Foo")
.add("postTime", "2019-01-01T12:12:12.000Z")
.build();
RestAssured.given()
.contentType(MediaType.APPLICATION_JSON)
.body(testObject.toString())
.when()
.post(getApiUri())
.then()
.contentType(MediaType.APPLICATION_JSON)
.statusCode(Response.Status.CREATED.getStatusCode());
}
}
在maven构建期间执行集成测试:thorntail服务器在集成前阶段启动,而在集成后阶段停止:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${version.maven-failsafe-plugin}</version>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<thorntail.test.host>${thorntail.bind.address}</thorntail.test.host>
<thorntail.test.port>${thorntail.http.port}</thorntail.test.port>
<thorntail.test.port-offset>${thorntail.port.offset}</thorntail.test.port-offset>
<thorntail.test.context-path>${thorntail.context.path}</thorntail.test.context-path>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.thorntail</groupId>
<artifactId>thorntail-maven-plugin</artifactId>
<version>${version.thorntail}</version>
<configuration>
<properties>
<thorntail.bind.address>${thorntail.bind.address}</thorntail.bind.address>
<thorntail.http.port>${thorntail.http.port}</thorntail.http.port>
<thorntail.port.offset>${thorntail.port.offset}</thorntail.port.offset>
<thorntail.context.path>${thorntail.context.path}</thorntail.context.path>
</properties>
</configuration>
<executions>
<execution>
<id>thorntail-package</id>
<goals>
<goal>package</goal>
</goals>
</execution>
<execution>
<id>thorntail-start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>thorntail-stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
问题是,我的集成测试使用两个不同的持久性单元。 DBUnit正确使用了H2持久性单元,但是使用默认的Postgres持久性单元来启动并初始化了thorntail服务器。
是否有可能在集成测试阶段与另一个持久性单元一起启动thorntail服务器?