title: How to init data in JUnit tests
body: <p>My task is to write stress (load) tests for Service Layer. There are mostly CRUD operations. We use JUnit as test framework, JUnitPerf for building load tests, Spring for injecting service beans and hibernate to access database.</p>
  
  <p>The stress test is something like: read entity - update entity - save - read again and compare. But to build the test I need some test data in the database, so I need to create this data before test and delete it after. The required process flow: create test data - run test in several threads - drop test data after all threads finish execution. There are a lot of test data, so it will be much better to use some test dump sql file to get it. So what I need is to: load data from file into database - perform stress test - delete all loaded data.</p>
  
  <p>I use SchemaExport to load data. I faced the following exception:</p>
  
  <pre><code>org.hibernate.HibernateException: No local DataSource found for configuration - 'dataSource' property must be set on LocalSessionFactoryBean
  at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.configure(LocalDataSourceConnectionProvider.java:49)
  at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
  at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
  at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(ManagedProviderConnectionHelper.java:27)
  at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:180)
  at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:133)
      .................
  </code></pre>
  
  <p>Here is the definition of my SessionFactory bean:</p>
  
  <pre><code>  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;value&gt;
              hibernate.dialect=${hibernate.dialect}
              hibernate.show.sql=${hibernate.show.sql}
          &lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
              ...
              my classes
              ...
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>And I init the test in the following way:</p>
  
  <pre><code> @BeforeClass
  public static void createTestData() throws AccessDeniedException, AccountException, SQLException {
      ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext("classpath:/applicationContext_test.xml");
      AnnotationSessionFactoryBean sessionFactoryBean = (AnnotationSessionFactoryBean) appCtx.getBean("sessionFactory");
      org.hibernate.cfg.Configuration configuration = sessionFactoryBean.getConfiguration();
      SchemaExport schemaExport = new SchemaExport(configuration);
      schemaExport.drop(false, true);
      schemaExport.create(false, true);
      if (schemaExport.getExceptions().size() &gt; 0) {
          for (Object exception : schemaExport.getExceptions()) {
              if (exception instanceof Throwable) {
                  ((Throwable) exception).printStackTrace();
              }
          }
          throw new IllegalStateException();
      }
  }
  </code></pre>
  
  <p>I mentioned that I need load test to make clear that I can't include data initialization in the test block.</p>
  
  <p>I have two questions:
  1) How can I init data before load test and delete it after?
  2) Am I on the right way? Or maybe I should switch to another technology for stress testing?</p>