Search code examples
testingmavenintegration-testingdbunit

integration tests with dbunit and jetty - dbunit not populating tables


Hi I'm trying to get an integration test working,
I'm using jetty as container and dbunit to populate a HSQLDB in memory database.
The code I'm using to populate the db with the dataset.xml file works since I use it in my unit test so if anyone could have a look at it and give me some advice it would be very much appreciated. here's the relevant part of the pom and my code.

pom.xml

<plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.26</version>
            <configuration>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
                <contextPath>/messages</contextPath>
                <connectors>
                    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                        <port>8080</port>
                        <maxIdleTime>60000</maxIdleTime>
                    </connector>
                </connectors>
                <webApp>
                    ${basedir}/target/messages
                </webApp>
                <scanIntervalSeconds>0</scanIntervalSeconds>
                <daemon>true</daemon>

                <scanTargetPatterns>
                    <scanTargetPattern>
                        <directory>
                           ${basedir}/target/test-classes/integrationtest/
                        </directory>
                        <includes>
                            <include>**/*.properties</include>
                            <include>**/*.xml</include>
                        </includes>
                    </scanTargetPattern>
                </scanTargetPatterns>
            </configuration>
            <executions>
                <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                    <version>1.1.1</version>
                </dependency>
                <dependency>
                    <groupId>commons-dbcp</groupId>
                    <artifactId>commons-dbcp</artifactId>
                    <version>1.2.2</version>
                </dependency>
                <dependency>
                    <groupId>org.hsqldb</groupId>
                    <artifactId>hsqldb</artifactId>
                    <version>2.2.8</version>
                </dependency>
            </dependencies>
        </plugin>



code:

 @BeforeClass
  public static void init() throws Exception {
Context ctx = new InitialContext();

ctx.createSubcontext("jdbc");

BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(org.hsqldb.jdbcDriver.class.getName());
dataSource.setUrl("jdbc:hsqldb:mem:MESSAGES");
dataSource.setUsername("sa");
dataSource.setPassword("");

ctx.bind("jdbc/messages", dataSource);

databaseTester = new DataSourceDatabaseTester(dataSource);
createTables(databaseTester.getConnection().getConnection());

databaseTester.setDataSet(getDataSet());
databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);

databaseTester.onSetup();

}

cheers


Solution

  • The integration tests are running in a different JVM from the jetty server, therefore an in-memory database is going to have different datasets for the integration test and for the Jetty service.

    Your best bet is to use an on-disk database in target/somedir and have both the tests and the servlet container access that database via the hsql prototcol.

    And change your jdbc uris to reference the server and port.

    To the above end, this plugin looks like it might be useful. Though the author has not published it into the central repository yet (the shame). You can probably use the exec-maven-plugin to start hsqldb if you cannot convince the author of that plugin to push it to central and you want a build that others can use

    An alternative is to have your test cases start & stop jetty themselves.