您好我正在尝试进行集成测试,
我使用码头作为容器和 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();
}
干杯