开发者

junit test case transaction not getting committed - no errors

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.!!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜