LogofaT
LogofaT

Reputation: 289

Java Integration testing with Arquillian, database cleanup

I am making some integration tests, and I would need to clean up the database between tests so i can make correct asserts, and also that the tests wont result in errors like Failed while seeding database or Unable to clean database..

pom:

<dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.jboss.arquillian</groupId>
    <artifactId>arquillian-bom</artifactId>
    <version>1.1.2.Final</version>
    <scope>import</scope>
    <type>pom</type>
   </dependency>
  </dependencies>
</dependencyManagement>

<dependency>
  <groupId>org.jboss.arquillian</groupId>
  <artifactId>arquillian-bom</artifactId>
  <version>1.1.2.Final</version>
  <type>pom</type>
</dependency>
<dependency>
   <groupId>org.jboss.arquillian.container</groupId>
   <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
   <version>1.0.0.CR4</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.jboss.arquillian.junit</groupId>
   <artifactId>arquillian-junit-container</artifactId>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.jboss.arquillian.extension</groupId>
   <artifactId>arquillian-persistence-api</artifactId>
   <version>1.0.0.Alpha5</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.jboss.arquillian.extension</groupId>
   <artifactId>arquillian-persistence-impl</artifactId>
   <version>1.0.0.Alpha5</version>
   <scope>test</scope>
</dependency>

Some test class:

@RunWith(Arquillian.class)
@UsingDataSet("datasets/empty.yml")
public class SomeServiceCase {

 @EJB
 SomeService someService;

 @Deployment
 public static JavaArchive createDeployment() {
     return ShrinkWrap.create(JavaArchive.class)
             .addPackages(true, "vo")
             .addPackages(true, "service")
             .addPackages(true, "domain")
             .addAsManifestResource("test-persistence.xml", ArchivePaths.create("persistence.xml"));
 }


 @Test
 @UsingDataSet("datasets/someModel/someModels.yml")
 @Cleanup(phase = TestExecutionPhase.AFTER, strategy = CleanupStrategy.STRICT)
 public void teastSomething() {
    //modifies database content here
 }

 @Test
 @UsingDataSet("datasets/someModel/someModels.yml")
 @Cleanup(phase = TestExecutionPhase.AFTER, strategy = CleanupStrategy.STRICT)
 public void testSomethingElse() {
    //needs initial database content for tests to work 
 }

Strict cleanup strategy should clear the database but it fails because of foreign-keys. So i tried disabling Referential Integrity by adding a property to arquillian.xml

<property name="initStatement">SET REFERENTIAL_INTEGRITY FALSE</property>

How can I achieve what I want? Any ideas?

Upvotes: 2

Views: 2561

Answers (3)

Robert
Robert

Reputation: 488

I like to solve this problem by using liquibase. It is quite easy to use as a maven plugin as follows:

                <plugin>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-maven-plugin</artifactId>

                    <executions>
                        <execution>
                            <id>update_test</id>
                            <phase>process-test-classes</phase>
                            <configuration>
                                <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                                <url>${test.db.connectionURL}</url>
                                <defaultSchemaName>${test.hibernate.default_schema}</defaultSchemaName>
                                <username>${test.db.username}</username>
                                <password>${test.db.password}</password>
                                <changeLogFile>db/db.changelog.xml</changeLogFile>
                                <contexts>test</contexts>
                                <changelogSchemaName>${test.hibernate.default_schema}</changelogSchemaName>
                            </configuration>
                            <goals>
                                <goal>dropAll</goal>
                                <goal>update</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

The goal that does the dropping and updating is:

<goal>dropAll</goal>
<goal>update</goal>

Upvotes: 0

Nils
Nils

Reputation: 111

You can set your database mode in the persistence.xml to drop-create. This will force the database to be rebuild from scratch for each redeploy (it's something!)

Upvotes: 1

MeBNoah
MeBNoah

Reputation: 175

you have to look closer in the stacktrace of the error message. Usually, from what I experienced, the database cannot be cleaned up because of foreign key constraints ... but the details should appear in the stack trace.

I don't know if your deactivation of the referential integrity works like that. I used the annotation @ApplyScriptBefore to disable it.

Upvotes: 0

Related Questions