title: Problems using DbUnit with Spring TestContext
body: <p><br>
  I'm trying to test my DAO layer (which is built on JPA) in separation. In the unit test, I'm using DbUnit to populate the database and Spring Test to get an instance of ApplicationContext.</p>
  
  <p>When I tried to use the SpringJunit4ClassRuner, the ApplicationContext got injected, but the DbUnit's getDataSet() method never got called.</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = "/testdao.xml")
  public class SimpleJPATest extends DBTestCase implements ApplicationContextAware {
      ...
  </code></pre>
  
  <p>Then I tried to remove the @RunWith annotation, that removed the problems with getDataSet() method. But now I no longer get ApplicationContext instance injected. I tried to use the @TestExecutionListeners annotation, which is supposed to configure the  DependencyInjectionTestExecutionListener by default, but the AppContext still doesn't get injected.</p>
  
  <pre><code>@TestExecutionListeners
  @ContextConfiguration(locations = "/testdao.xml")
  public class SimpleJPATest extends DBTestCase implements ApplicationContextAware {
      ...
  </code></pre>
  
  <p>Does anyone have any ideas? Is it generally a bad idea to combine these two frameworks?</p>
  
  <hr>
  
  <p>EDIT: here is the rest of the source for the test class:</p>
  
  <pre><code>@TestExecutionListeners
  @ContextConfiguration(locations = "/testdao.xml")
  public class SimpleJPATest extends DBTestCase implements ApplicationContextAware {
  
      static final String TEST_DB_PROPS_FILE = "testDb.properties";
      static final String DATASET_FILE = "testDataSet.xml";
      static Logger logger = Logger.getLogger( SimpleJPATest.class );
      private ApplicationContext ctx;
  
      public SimpleJPATest() throws Exception {
          super();
          setDBUnitSystemProperties(loadDBProperties());
      }
  
      @Test
      public void testSimple() {
          EntityManagerFactory emf = ctx.getBean("entityManagerFactory", EntityManagerFactory.class);
          EntityManager em = emf.createEntityManager();
          GenericDAO&lt;Club&gt; clubDAO = new JpaGenericDAO&lt;Club&gt;(ClubEntity.class, "ClubEntity", em);
          em.getTransaction().begin();
          Collection&lt;Club&gt; allClubs = clubDAO.findAll();
          em.getTransaction().commit();
          assertEquals(1, allClubs.size());
      }
  
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
           this.ctx = applicationContext;
      }
  
      private void setDBUnitSystemProperties(Properties props) {
          System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS,
                  props.getProperty("db.driver"));
          System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL,
                  props.getProperty("db.url"));
          System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME,
                  props.getProperty("db.username"));
          System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD,
                  props.getProperty("db.password"));
  
      }
  
      private Properties loadDBProperties() throws Exception {
          URL propsFile = ClassLoader.getSystemResource(TEST_DB_PROPS_FILE);
          assert (propsFile != null);
          Properties props = new Properties();
          props.load(propsFile.openStream());
          return props;
      }
  
      @Override
      protected void setUpDatabaseConfig(DatabaseConfig config) {
          config.setProperty( DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
              new HsqldbDataTypeFactory() );
      }
  
      @Override
      protected DatabaseOperation getSetUpOperation() throws Exception {
          return DatabaseOperation.CLEAN_INSERT;
      }
  
      @Override
      protected DatabaseOperation getTearDownOperation() throws Exception {
          return DatabaseOperation.DELETE_ALL;
      }
  
      @Override
      protected IDataSet getDataSet() throws Exception {
          logger.debug("in getDataSet");
          URL dataSet = ClassLoader.getSystemResource(DATASET_FILE);
          assert (dataSet != null);
          FlatXmlDataSet result = new FlatXmlDataSetBuilder().build(dataSet);
          return result;
      }
  }
  </code></pre>