views:

30

answers:

2

Hi,

We have implemented JUnit4 in our application which uses Spring core & JPA with DB2. We need to test a full functionality which retrieves data from one database and merges into another database.

Test case for retrieving the data from 1st database is written and it is running without any error perfectly but the records are not store into the 2nd database.

Implementation

The TestCase class we have included the following annotations to make the test case run under a transaction if necessary,

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
      DependencyInjectionTestExecutionListener.class,    
      TransactionalTestExecutionListener.class})
@ContextConfiguration(locations={""})
@TransactionConfiguration(transactionManager="defaultTransactionManager", defaultRollback=false)
@Transactional

In the application we have a manager class to perform this operation with doSynch() method. From that method crudHelper class's txStore() method will be called to initialize and call doStore() method (in same class) to merge the entity to database.

Following are the transactional declarative through out this test case logic

TestCase testSynch() - @Transactional(propagation=Propagation.SUPPORTS)
Manager doSynch() - @Transactional(propagation=Propagation.NEVER)
CRUDHelper txStore() - @Transactional(propagation=Propagation.REQUIRED)
           doStore() - No Transactional annotation

doSynch() is marked as NEVER as at that point it doesn't need any transaction and in further levels such as in CRUDHelper the transaction can be marked as REQUIRED to ensure a transaction to be available.

Problem

Here when we run the test case which calls the Manager's doSynch() method to test the functionality, complete flow is working perfect except that records are not merged and no errors are thrown.

The Manager method when called from a JSP works great. Also we tested by calling txStore() directly from test case and it also works fine.

Please let us know whether the transaction management is not proper or a work around to this issue will be of greater help. Also pls update me if the issue or environment is not clear. Thanks in advance.!!

+1  A: 

Do you mark your methods with the @Rollback annotation?

From the JavaDoc:

Test annotation to indicate whether or not the transaction for the annotated test method should be rolled back after the test method has completed. If true, the transaction will be rolled back; otherwise, the transaction will be committed.

Espen
@Espen: Thanks 4 ur reply. We have set defaultRollback value as false in TestCase class level and not given any other rollback for method levels. But only case how it works is by adding @Transactional to doStore() method. We are able to see insert queries printed only in this case and it doStore's annotation is remove not even a single insert query is printed when calling em.merge().But we couldnt do that change as it is in our freezed framework class. Any help would be appreciable.!!
techastute
+1  A: 

I'm not sure if it works for DB however here some guy has almost the same problem, and finally he found out that the issue caused by Hibernate, which didn't flush its session on time.

wax