我的任务是为服务层编写压力(负载)测试。大部分都是CRUD操作。我们使用JUnit作为测试框架,JUnitPerf用于构建负载测试,Spring用于注入服务beans,hibernate用于访问数据库。
压力测试类似于:读取实体-更新实体-保存-再次读取并进行比较。但是为了构建测试,我需要数据库中的一些测试数据,所以我需要在测试之前创建这些数据,并在测试之后删除它。所需的流程:创建测试数据-在多个线程中运行测试-在所有线程完成执行后删除测试数据。有大量的测试数据,所以最好使用一些测试转储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)我的方法正确吗?或者我应该切换到另一种技术进行压力测试?
发布于 2011-04-02 02:33:04
您可以使用DBUnit为您加载数据。或者使用集成了DBUnit、Spring和Hibernate的Unitils。
致以敬意,
发布于 2011-04-02 03:23:33
如果您使用Spring进行测试,则应该考虑不使用Spring Testing Support,而不是自己加载应用程序上下文并查找bean。
您可以在测试类上执行类似的操作,并自动注入bean。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"applicationContext_test.xml"})关于测试后的“清理”,我总是想在测试范围内使用Spring transaction management。应该可以将测试开始声明为@Transactional,并在测试后以编程方式回滚事务。不需要以这种方式显式删除数据。
到目前为止,这只是一个想法。一定要自己试一试...
https://stackoverflow.com/questions/5517306
复制相似问题