2

I'm using DBUnit for testing DAOs implemented with Hibernate and Spring. I put a breakpoint in the setup code of DBUnit and I see that the corresponding methods are never called. I need to initialize my database following the CLEAN_INSERT scheme but the setup method (overriden from TestCase) is never called.

Any idea?

EDIT: I saw in the maven dependency graph that DBunit 2.4.8 (last version) depends on JUnit 3.8.2?! Is DBUnit compatible with newer versions of JUnit (4.9)?

Thanks

manash
  • 6,985
  • 12
  • 65
  • 125

2 Answers2

3

If you're using JUnit 4, you shouldn't be extending from TestCase any more. Put the code from the example in http://www.dbunit.org/howto.html#noextend in your own method. You can call it anything you want as long as you annotate it with @org.junit.Before.

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • I didn't precise that I'm using the first method, i.e. extending from DBTestCase class. In this case, I'm not supposed to define setup and teardown methods. – manash Mar 30 '12 at 13:32
  • I've never used `DBTestCase`, which is based on JUnit 3 code and requires that you set up the associated framework for JUnit 3 rather than using the way JUnit 4 handles these things. Instead, you can write your own parent class that handles database setup, teardown, and uses the CLEAN_INSERT to reload the data set. – David H. Clements Mar 30 '12 at 13:46
1

You can certainly use DBUnit with JUnit 4.x. I've tried to whip up an example using vanilla JDBC leaving the Spring and Hibernate to you. Although, I highly recommend giving this open source utility a try to make your Spring and DBUnit experience much more enjoyable. Note, I haven't contributed to that utility, just used it.

Another DBUnit challenge is that it does not create tables for you, so an in-memory database can give you issues, take a look at this other stackoverflow post for more details there.

I'm using DBUnit 2.4.8, HSQLDB 1.8.0.10, and JUnit 4.10.

import static org.junit.Assert.assertEquals;

import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DBUnitTester {

  private IDatabaseTester databaseTester;

  @Before
  public void init() throws Exception {
    databaseTester = new JdbcDatabaseTester(org.hsqldb.jdbcDriver.class.getName(), "jdbc:hsqldb:sample", "sa", "");

    createTablesSinceDbUnitDoesNot(databaseTester.getConnection().getConnection());

    String inputXml = 
        "<dataset>" + 
        "    <TEST_TABLE COL0=\"row 0 col 0\" " + 
        "                COL1=\"row 0 col 1\"" + 
        "                COL2=\"row 0 col 2\"/> " + 
        "</dataset>";
    IDataSet dataSet = new FlatXmlDataSetBuilder().build(new StringReader(inputXml));
    databaseTester.setDataSet(dataSet);
    databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
    databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
    databaseTester.onSetup();
  }

  private void createTablesSinceDbUnitDoesNot(Connection connection) throws SQLException {
    PreparedStatement statement = connection.prepareStatement("CREATE TABLE TEST_TABLE(COL0 VARCHAR(20), COL1 VARCHAR(20), COL2 VARCHAR(20))");
    statement.execute();
    statement.close();

  }

  @Test
  public void firstTest() throws SQLException, Exception {
    PreparedStatement statement = databaseTester.getConnection().getConnection().prepareStatement("SELECT COL0 FROM TEST_TABLE");
    ResultSet rs = statement.executeQuery();
    rs.next();
    assertEquals("row 0 col 0", rs.getString("COL0"));
  }


  @After
  public void cleanUp() throws Exception {
    databaseTester.onTearDown();
  }
}
Community
  • 1
  • 1