Announcement Announcement Module
Collapse
No announcement yet.
Hibernate and DBUnit (How do I do it?) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate and DBUnit (How do I do it?)

    hello to all.
    i've read this article on TSS.
    http://www.theserverside.com/tt/arti...pringandDBunit

    and i don't get it.

    at the momment, i use the real database connection
    and i clean the tables before and after running the tests.

    for example
    i have this class

    Code:
    package com.wikidprice.model.dao.hibernate;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNull;
    
    import javax.sql.DataSource;
    
    import org.apache.commons.lang.StringUtils;
    import org.gienah.testing.junit.Configuration;
    import org.gienah.testing.junit.Dependency;
    import org.gienah.testing.junit.SpringRunner;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.dao.DataIntegrityViolationException;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.test.AssertThrows;
    
    import com.wikidprice.model.dao.BrandDAO;
    import com.wikidprice.model.entity.Brand;
    
    @RunWith(value = SpringRunner.class)
    @Configuration(locations = { "spring-beans.xml" })
    public class TestHibernateBrandDAO {
    
    	private static final String COKE = "Coke";
    
    	private static final String PEPSI = "Pepsi";
    
    	private final String TOO_LONG = StringUtils.repeat(PEPSI, 100);
    
    	@Dependency
    	private DataSource dataSource;
    
    	@Dependency
    	private BrandDAO brandDAO;
    
    	@Before
    	@After
    	public void init() {
    		final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    		jdbcTemplate.execute("truncate table brand");
    	}
    
    	@Test
    	public void testAddGood() {
    		final Brand brand = new Brand();
    		brand.setDescription(PEPSI);
    		brandDAO.add(brand);
    		brand.setDescription(COKE);
    		brandDAO.add(brand);
    		final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    		final String sqlCount = "select count(*) from brand";
    		final int count = jdbcTemplate.queryForInt(sqlCount);
    		assertEquals(2, count);
    	}
    
    	@Test
    	public void testAddTooLong() {
    		final Brand brand = new Brand();
    		brand.setDescription(TOO_LONG);
    		new AssertThrows(DataIntegrityViolationException.class) {
    			public void test() throws Exception {
    				brandDAO.add(brand);
    			}
    		}.runTest();
    	}
    
    	@Test
    	public void testGetGood() {
    		final Brand brand = new Brand();
    		brand.setDescription(PEPSI);
    		brandDAO.add(brand);
    		brand.setDescription(COKE);
    		brandDAO.add(brand);
    		Brand dbBrand;
    		dbBrand = brandDAO.find(new Integer(2));
    		assertEquals(COKE, dbBrand.getDescription());
    		dbBrand = brandDAO.find(new Integer(1));
    		assertEquals(PEPSI, dbBrand.getDescription());
    	}
    
    	@Test
    	public void testGetBad() {
    		final Brand brand = new Brand();
    		brand.setDescription(PEPSI);
    		brandDAO.add(brand);
    		brand.setDescription(COKE);
    		brandDAO.add(brand);
    		Brand dbBrand;
    		dbBrand = brandDAO.find(new Integer(100));
    		assertNull(dbBrand);
    	}
    }
    how do i use dbunit testing here.
    the dao are of course hibernate daos.

    personally, i really hate it when people ask "how do i use xxx",
    but this time i did my research,
    i've read the article on TSS, i've read docs on DBUnit's website, there are no examples on spring + junit 4 + hibernate.

  • #2
    I actually posted that link on the meta forum and there was some feedback.
    http://forum.springframework.org/showthread.php?t=42687

    Personally, I don't use it, I haven't really had a need to. I guess if you want to setup lots of data it might be useful. What is it you are struggling to understand? What is it you want to do?

    Comment


    • #3
      i don't understand how my hibernatedao classes use dbunit instread of the real database. hibernatedaos use sessionFactory, and sessionFactory uses dataSource.
      do i replace the datasource with dbunit's classes?
      Last edited by titiwangsa; Aug 21st, 2007, 10:31 PM.

      Comment


      • #4
        I'll have to re-read the article, but I don't think that was actually what it was suggesting. It was simply saying here's a way of inserting lots of data with a tool instead of having to programmatically build all the data in the database.

        Comment


        • #5
          i guess that explain it. i must have mis understood the article. hahah.
          my bad.

          Comment


          • #6
            Originally posted by titiwangsa View Post
            i guess that explain it. i must have mis understood the article. hahah. my bad.
            , at least it gave me a reason to read it again.

            Comment

            Working...
            X