I'm using Java 8, JUnit 4.12, Spring 4, and Hibernate 5.2.12.Final. I want to test a Hibernate method where I update some rows. The method looks like this
final CriteriaBuilder qb = m_entityManager.getCriteriaBuilder();
final CriteriaUpdate<FailedEvent> q = qb.createCriteriaUpdate(FailedEvent.class);
final Root<FailedEvent> root = q.from(FailedEvent.class);
q.where(qb.and( qb.equal(root.get(FailedEvent_.objectId), objectId) ));
q.set(root.get(FailedEvent_.flaggedForDelete), true);
affectedRows = m_entityManager.createQuery(q).executeUpdate();
return affectedRows > 0;
I have the following JUnit test to verify this
public class FailedEventDaoTest extends AbstractTransactionalJUnit4SpringContextTests
...
@Test
public final void testFlagForDeleteByObjectId()
{
final String eventId = "testId";
final FailedEvent event = failedEventDao.findByEventId(eventId);
Assert.assertFalse("A pre-condition fo this test is that an failed_event record with id \"" + eventId + "\" have a non-empty object id.", StringUtils.isEmpty(event.getObjectId()));
Assert.assertTrue(failedEventDao.flagForDeleteByObjectId(event.getObjectId()));
final FailedEvent foundEvent = failedEventDao.findByEventId(eventId);
Assert.assertTrue("Failed to mark flag for deletion.", foundEvent.getFlaggedForDelete());
} // testFlagForDeleteByObjectId
But my test fails on the last line,
Assert.assertTrue("Failed to mark flag for deletion.", foundEvent.getFlaggedForDelete());
Is this because the transaction hasn't completed yet so nothing has been updated in the database? What's the way I can validate that the right records are updated?
Load the test configuration file before starting test-suite. @ContextConfiguration (locations = "classpath:application-context-test. xml" ); As you have loaded the main configuration as well, you will be easily able to inject DAO references directly into testcase.
Spring's integration testing support has the following primary goals: To manage Spring IoC container caching between tests. To provide Dependency Injection of test fixture instances. To provide transaction management appropriate to integration testing.
@Transaction annotation for integration testing in Spring Boot.
Set the spring ApplicationContext for your test classes using @ContextConfiguration annotation.
Try @AfterTransaction Annotation to test the result.
@AfterTransaction
public final void theRealTest()
{
Assert.assertTrue("Failed to mark flag for deletion.", foundEvent.getFlaggedForDelete());
}
https://www.springbyexample.org/examples/simple-spring-transactional-junit4-test-code-example.html
if this is also not working try:
"So, you need to create multiple transactions inside your test method. As you can see, you cannot use AbstractTransactionalJUnit4SpringContextTests, because it creates a single transaction for the whole test method, and cannot use bare AbstractJUnit4SpringContextTests, because it creates no transactions at all.
The solution is to use AbstractJUnit4SpringContextTests and manage transactions inside your test method programmatically.
You need to inject PlatformTransactionManager into your test, create TransactionTemplate from it and use it to demarcate your transactions as described in 11.6 Programmatic transaction management."
found here: Using AbstractTransactionalJUnit4SpringContextTests with commit halfway through
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With