Results 1 to 4 of 4

Thread: dbunit and AbstractTransactionalDataSourceSpringContextTests

  1. #1
    Join Date
    May 2005
    Location
    San Diego, CA
    Posts
    76

    Default dbunit and AbstractTransactionalDataSourceSpringContextTests

    I am trying to use dbunit with AbstractTransactionalDataSourceSpringContextTests to write an end to end integration test. I am using hibernate as the persistence framwork. I am running into problems where hibernate is not flushing to the db so that my tests fail when i assert through dbunit. I think this has to do with hibernate set to auto flush.

    I really don't want to do an explict flush in my dao nor do i want the hibernate session to be set to eager flush, and even that doesn't seem to work when i do a delete, which is odd.

    Has anyone else run into this problem? I am using hibernate 3.0.2 and spring 1.2rc2.

    Also I do want to know whether this stratergy of checking the result is the norm or do people just use hibernate to test the result. If I use hibernate to test the result everything works fine.

    Thanks,
    Umesh

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Dependency inject your SessionFactory into your test subclass and create a HibernateTemplate instance variable. Call flush on that in your test methods so you don't need to change your application code.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    May 2005
    Location
    San Diego, CA
    Posts
    76

    Default

    rod i did try that but i am running into a problem, once i do a flush the transaction doesn't get rolled back, and i have to set a variable in the test so that in onSetUpInTransaction i do a clean insert which defeats the purpose of using AbstractTransactionalDataSourceSpringContextTests.
    public void testSaveRestaurant() throws Exception
    {
    Restaurant newRestaurant = new Restaurant();
    newRestaurant.setLogoUri("applebee.jpg");
    newRestaurant.setName("applebee");
    restaurantService.saveRestaurant(newRestaurant);
    cleanInsertFlag = true;
    hibernateTemplate.flush();
    List results = jdbcTemplate.queryForList("select name,logouri from restaurant where name ='applebee'");
    assertNotNull(results);
    assertEquals(1,results.size());
    Map columns = (HashMap)results.get(0);
    String name = (String)columns.get("name");
    String logouri = (String)columns.get("logouri");
    assertEquals("applebee",name);
    assertEquals("applebee.jpg",logouri);

    }

    protected void onSetUpInTransaction() throws Exception {
    // TODO Auto-generated method stub
    super.onSetUpInTransaction();

    if (cleanInsertFlag)
    {
    // initialize your database connection here
    connection = new DatabaseDataSourceConnection(jdbcTemplate.getDataS ource());
    // ...

    // initialize your dataset here
    IDataSet dataSet = new XmlDataSet(getClass().getResourceAsStream("Restaur antDataSet.xml"));
    // ...
    DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
    }

    }

  4. #4
    Join Date
    May 2005
    Location
    San Diego, CA
    Posts
    76

    Default solved the test problems

    well it was mixture of things:
    1) innodb/bdb table types in mysql are allowed to have transactions and the myisam table type does not.
    2) switched the datasource to org.apache.commons.dbcp.BasicDataSource
    according to a previous rj post and switched off autocommit in the
    connection pool.

    i was running into lock problems with dbunit and decided to use jdbcTemplate to prepopulate the database using onSetUpInTransaction.

Similar Threads

  1. Replies: 3
    Last Post: Jul 19th, 2011, 08:42 AM
  2. Replies: 3
    Last Post: Oct 22nd, 2005, 04:44 PM
  3. Replies: 7
    Last Post: Aug 18th, 2005, 02:41 PM
  4. Using DbUnit.
    By sherihan in forum Data
    Replies: 5
    Last Post: May 12th, 2005, 02:27 AM
  5. Replies: 3
    Last Post: Apr 19th, 2005, 06:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •