Announcement Announcement Module
Collapse
No announcement yet.
dbunit and AbstractTransactionalDataSourceSpringContextTests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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);
      }

      }

      Comment


      • #4
        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.

        Comment

        Working...
        X