Search code examples
springspring-transactionstransactionalspring-testspring-test-dbunit

Commiting transaction within test with Spring test


I am testing my repository for update operation

@Test

    public void updateStatusByEmailWithEmailCustomer()
    {
        customerQuickRegisterRepository.save(standardEmailCustomer());

        assertEquals(CUST_STATUS_EMAIL,customerQuickRegisterRepository.findByEmail(CUST_EMAIL).getStatus());

        customerQuickRegisterRepository.updateStatusByEmail(CUST_EMAIL,STATUS_EMAIL_VERFIED ); 

        assertEquals(STATUS_EMAIL_VERFIED,customerQuickRegisterRepository.findByEmail(CUST_EMAIL).getStatus());
    }

In the test case i saving my entity with default status and after that i am changing it to other using updateStatusByEmail(CUST_EMAIL,STATUS_EMAIL_VERFIED ) but still next assert statement is failing, this is due to fact that the updates during the test execution are commited after completion of test....Is there any way that I can commit my changes within the test?


Solution

  • You can likely get by with flushing the underlying Hibernate Session, as this will push your changes to the underlying tables in the database (within the current test-managed transaction).

    Search for "false positives" in the testing chapter of the Spring reference manual for details.

    Basically, you will want to call flush() on the current Hibernate Session after your update call and before the corresponding assertion.

    If that does not solve your problem, with Spring Framework 4.1, you can use the new TestTransaction API for programmatic transaction management within tests.

    Regards,

    Sam (lead of the spring-test module)