title: junit 4 TransactionalTestExecutionListener insert test data only once for all tests in class?
body: <p>I have a junit 4 test class testing a DAO.</p>
  
  <p>unit test:</p>
  
  <pre><code>  @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(locations = {
        "classpath:/WEB-INF/applicationContext-db.xml",
        "classpath:/WEB-INF/applicationContext-hibernate.xml",
        "classpath:/WEB-INF/applicationContext.xml" })
      @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})
      @DataSetLocation("test/java/com/yada/yada/dao/dbunit-general.xml")
      @TransactionConfiguration(transactionManager="transactionManager", defaultRollback = true)
      @Transactional
      public class RealmDAOJU4Test {
  
       @Autowired
       private DbUnitInitializer dbUnitInitializer;
  
       @Autowired
       private RealmDAO realmDAO;
  
       @BeforeTransaction
       public void setupDatabase() {
         // use dbUnitInitializer to insert test data
       }
  
       @Test
       public void testGetById() {
        Integer id = 2204;
        Realm realm = realmDAO.get(id);
        assertEquals(realm.getName().compareToIgnoreCase(
          "South Technical Realm"), 0);
        assertEquals(8, realm.getRealmRelationships().size());
       }
  
       // more test methods annotated here
  }
  </code></pre>
  
  <p>The @BeforeTransacation method runs before EVERY test method.  What I would like to do is: use my DbUnitInitializer to load data into my database - ONCE when the class is created. Then have each test in the class do what it needs to do with the database, then roll back (not commit) it's changes. It seems over kill to re-insert all the same data from my test files before EVERY test. Is there a way to accomplish this?</p>
  
  <p>or</p>
  
  <p>Is the <em>correct</em> way to write these tests to completely load the database before EVERY test?  If so, what function does the defaultRollback=true have in this situation?</p>
  
  <p>thanks for helping me along in my thinking... </p>