簡體   English   中英

與dbunit和jetty的集成測試-dbunit未填充表

[英]integration tests with dbunit and jetty - dbunit not populating tables

嗨,我正在嘗試進行集成測試,
我使用碼頭作為容器和dbunit在內存數據庫中填充HSQLDB。
自從我在單元測試中使用它以來,我用dataset.xml文件填充數據庫的代碼就可以使用,因此,如果有人可以看一下並給我一些建議,將不勝感激。 這是pom和我的代碼的相關部分。

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>



碼:

 @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();

}

干杯

集成測試在與Jetty服務器不同的JVM中運行,因此內存數據庫將具有用於集成測試和Jetty服務的不同數據集。

最好的選擇是使用target/somedir的磁盤數據庫,並使測試和servlet容器都通過hsql協議訪問該數據庫。

並更改您的jdbc uri以引用服務器和端口。

為了達到上述目的,此插件看起來可能很有用。 盡管作者尚未將其發布到中央存儲庫(可恥)。 如果您無法說服該插件的作者將其推送到中央,並且希望其他人可以使用的構建,則可以使用exec-maven-plugin啟動hsqldb。

一種替代方法是讓您的測試用例自己啟動和停止碼頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM