2011-04-01 317 views
4

我的任务是编写服务层的压力(负载)测试。大多数是CRUD操作。我们使用JUnit作为测试框架,JUnitPerf用于构建负载测试,Spring用于注入服务bean并通过hibernate访问数据库。如何在JUnit测试中初始化数据

压力测试类似于:读取实体 - 更新实体 - 保存 - 再次读取并比较。但为了构建测试,我需要在数据库中使用一些测试数据,所以我需要在测试之前创建这些数据并在之后将其删除。所需的处理流程:创建测试数据 - 在多个线程中运行测试 - 在所有线程完成执行后丢弃测试数据。有很多测试数据,所以使用一些测试转储sql文件来获取它会好得多。所以我需要的是:将数据从文件加载到数据库中 - 执行压力测试 - 删除所有加载的数据。

我使用SchemaExport来加载数据。我遇到以下异常:

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

这里是我的SessionFactory的bean定义:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="hibernateProperties"> 
     <value> 
      hibernate.dialect=${hibernate.dialect} 
      hibernate.show.sql=${hibernate.show.sql} 
     </value> 
    </property> 
    <property name="annotatedClasses"> 
     <list> 
      ... 
      my classes 
      ... 
     </list> 
    </property> 
</bean> 

我初始化以下列方式测试:

@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() > 0) { 
     for (Object exception : schemaExport.getExceptions()) { 
      if (exception instanceof Throwable) { 
       ((Throwable) exception).printStackTrace(); 
      } 
     } 
     throw new IllegalStateException(); 
    } 
} 

我提到了我需要进行负载测试以明确我不能在测试块中包含数据初始化。

我有两个问题: 1)如何在加载测试之前初始化数据并删除它之后? 2)我是否正确?或者,也许我应该换用其他技术来进行压力测试?

回答

1

您可以使用DBUnit为您做数据加载。或者使用Unitils,它将DBUnit和Spring与Hibernate集成在一起。

问候,

+0

我已经安装了Unitils,但是它在每个方法调用中都执行了DataSet插入。我需要它加载一次,然后在多个线程中执行测试方法,而无需任何额外的数据加载。 – Zalivaka 2011-04-04 10:25:42

0

如果你使用Spring测试你应该考虑不使用Spring Testing Support而不是加载自己的应用程序上下文和查找豆。

你可以在你的测试类上做这样的事情并自动注入bean。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"applicationContext_test.xml"}) 

关于“清理”的测试,我一直以为有关使用Spring transaction management为测试范围之后。应该可以将测试开始声明为@Transactional,并在测试后以编程方式回滚事务。没有必要用这种方式显式删除数据。

这只是一个想法。必须自己试一试...