My task is to write stress (load) tests for the service level. Mostly CRUD operations are performed. We use JUnit as a test environment, JUnitPerf to build load tests, Spring to inject beans service, and hibernate to access the database.
A stress test is something like: reading an entity - updating an entity - saving - reading again and comparing. But to build the test, I need some test data in the database, so I need to create this data before testing and delete it after. Necessary process: create test data - run the test in multiple threads - discard the test data after all threads have completed. There is a lot of test data, so it would be much better to use some kind of SQL test dump file to get it. Therefore, I need: upload data from a file to a database - perform a stress test - delete all downloaded data.
I use SchemaExport to load data. I came across the following exception:
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) .................
Here is the definition of my 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>
And I run the test as follows:
@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(); } }
I mentioned that I need a load test to make it clear that I cannot enable data initialization in the test block.
I have two questions: 1) How can I initialize data before downloading and delete it after? 2) Am I right? Or maybe I need to switch to another technology for stress testing?