Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 984
raksja Avatar asked Aug 17 '10 06:08

raksja


People also ask

Why is JUnit ignoring tests?

Sometimes you may require not to execute a method/code or Test Case because coding is not done fully. For that particular test, JUnit provides @Ignore annotation to skip the test.

What is the difference between errors and failures in JUnit?

Both failure and error in JUnit tests indicate an undesired situation, but their semantics are different. Failures notify of an invalid test result, errors indicate an unexpected test execution.

What is positive and negative test cases in JUnit?

This is one of the software testing technique in which the test cases are designed to include values at the boundary. If the input data is used within the boundary value limits, then it is said to be Positive Testing. If the input data is picked outside the boundary value limits, then it is said to be Negative Testing.

Which of these annotations will you use for automatically rolling back a transaction after a test?

@Rollback is a test annotation that is used to indicate whether a test-managed transaction should be rolled back after the test method has completed.


1 Answers

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.

like image 193
Espen Avatar answered Oct 13 '22 11:10

Espen