gpt4 book ai didi

testing - 使用 dbunit 和 jetty 进行集成测试 - dbunit 不填充表

转载 作者:行者123 更新时间:2023-11-28 20:57:03 25 4
gpt4 key购买 nike

您好,我正在尝试进行集成测试,
我使用码头作为容器和 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 协议(protocol)访问该数据库。

并更改您的 jdbc uris 以引用服务器和端口。

为了上述目的,这plugin看起来它可能有用。虽然作者还没有将它发布到中央存储库(耻辱)。如果您不能说服该插件的作者将其推送到中心并且您想要一个其他人可以使用的构建,您可能可以使用 exec-maven-plugin 来启动 hsqldb

另一种方法是让您的测试用例自己启动和停止 jetty。

关于testing - 使用 dbunit 和 jetty 进行集成测试 - dbunit 不填充表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12243608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com