How to start data in JUnit tests

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?

+4
source share
2 answers

You can use DBUnit to load data for you. Or use Unitils , which combines DBUnit with Spring with Hibernate.

Hello,

+1
source

If you use Spring for testing, you should not use Spring Testing support instead of loading the application context for yourself and find the beans.

You can do something similar in your test class and automatically insert beans.

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

Regarding the β€œcleanup” after the test, I always thought about using Spring transaction management for the validation area. It should be possible to declare the start of the test as @Transactional and rollback the transaction programmatically after the test. There is no need to explicitly delete data.

This is just an idea so far. Gotta give it a try ...

0
source

Source: https://habr.com/ru/post/1346282/


All Articles