2012-09-03 64 views
1

嗨我试图让集成测试工作,
我使用jetty作为容器和dbunit来填充内存数据库中的HSQLDB。
我用来填充数据库与dataset.xml文件的代码工作,因为我在我的单元测试中使用它,所以如果任何人都可以看看它,并给我一些建议,将非常感激。 这里是pom和我的代码的相关部分。

的pom.xml与dbunit和jetty的集成测试 - dbunit没有填充表

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

}

欢呼

回答

0

集成测试运行在与码头服务器不同的JVM中,因此内存数据库将具有用于集成测试和Jetty服务的不同数据集。

最好的办法是使用target/somedir中的磁盘数据库,并让测试和servlet容器都通过hsql prototcol访问该数据库。

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

为了达到上述目的,plugin看起来可能有用。尽管作者还没有将它发布到中央存储库(羞耻)中。如果你不能说服该插件的作者将它推到中央,并且你想要一个其他人可以使用的版本,你可以使用exec-maven-plugin来启动hsqldb。

另一种方法是让你的测试用例开始&自己停止码头。

+0

谢谢你的建议,但是当我试图在磁盘上的数据库使用我获得以下错误: 重度:不能重新打开数据库 org.hsqldb.HsqlException:文件输入/输出错误:目标/ DB/MESSAGES.log –

+0

是的,谢谢,很好,我相应地纠正了答案 –