2
votes

while testing a DAO function in spring+hibernate application, data rollback is not happening. I am trying to do the rollback a junit test case by @Rollback annotation

transactionManager config:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf" />
</bean>

In my main code I am using hibernateTemplate() to do CRUD operations

test code:

@Test
@Transactional
@Rollback(true)
public void test_populateObject() throws Exception {


    // TransactionDefinition def = new DefaultTransactionDefinition();
    // TransactionStatus status = transactionManager.getTransaction(def);

    manualObj= new ManualObj();

    manualObj.setName("TestObj001");
    manualObj.setRefId("TestObj001");
    manualObj.setCallback("TestObj001");
    m
    TestObject obj=orderService.createTestObject(manualObj);

    Assert.assertNotNull(obj);

    // transactionManager.rollback(status);

}

The created object is not being deleted by rollback.

So, What could be the reason that @Rollback is n't happening?

Edit 1:

I am using Spring Data JPA and hibernateTemplate based data access in same application. Both using the same transaction manager specified like the above bean config.

1

1 Answers

2
votes

Did you configure your test to use the xml configuration, and did you specify the SpringJUnit4ClassRunner to run your test? You can do it like this:

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = {"testContext.xml"})
@Transactional
public class YourTest{ ... }