Java 如何在JUnit测试中初始化数据
我的任务是为服务层编写压力(负载)测试。主要是积垢作业。我们使用JUnit作为测试框架,JUnitPerf用于构建负载测试,Spring用于注入服务bean,hibernate用于访问数据库 压力测试类似于:读取实体-更新实体-保存-再次读取并比较。但是为了构建测试,我需要数据库中的一些测试数据,所以我需要在测试之前创建这些数据,并在测试之后删除它。所需的流程:创建测试数据-在多个线程中运行测试-在所有线程完成执行后删除测试数据。有很多测试数据,所以最好使用一些测试转储sql文件来获取它。所以我需要的是:将数据从文件加载到数据库-执行压力测试-删除所有加载的数据 我使用SchemaExport加载数据。我面临以下例外情况:Java 如何在JUnit测试中初始化数据,java,hibernate,spring,junit,stress-testing,Java,Hibernate,Spring,Junit,Stress Testing,我的任务是为服务层编写压力(负载)测试。主要是积垢作业。我们使用JUnit作为测试框架,JUnitPerf用于构建负载测试,Spring用于注入服务bean,hibernate用于访问数据库 压力测试类似于:读取实体-更新实体-保存-再次读取并比较。但是为了构建测试,我需要数据库中的一些测试数据,所以我需要在测试之前创建这些数据,并在测试之后删除它。所需的流程:创建测试数据-在多个线程中运行测试-在所有线程完成执行后删除测试数据。有很多测试数据,所以最好使用一些测试转储sql文件来获取它。所以我
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)
.................
下面是我的SessionFactorybean的定义:
<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>
我提到我需要进行负载测试,以明确我不能在测试块中包含数据初始化
我有两个问题:
1) 如何在负载测试之前初始化数据并在测试之后删除它?
2) 我走对了吗?或者我应该换一种技术进行压力测试?你可以用它来为你加载数据。或者使用将DBUnit与Spring和Hibernate集成在一起的
问候语,
< p>如果你使用Spring进行测试,你应该考虑不要使用,而不是为自己加载应用程序上下文和查找bean。 您可以在测试类上执行类似的操作,并自动注入bean@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"applicationContext_test.xml"})
关于测试后的“清理”,我一直在考虑在测试范围内使用。应该可以将测试开始声明为@Transactional
,并在测试后以编程方式回滚事务。不需要以这种方式删除显式数据
到目前为止,这只是一个想法。必须自己尝试一下…我有设置单元,但它会在每次方法调用时执行数据集插入。我需要它被加载一次,然后在几个线程中执行测试方法,而不需要任何额外的数据加载。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"applicationContext_test.xml"})