Ok so today I was working on some JUnit tests within Spring using AbstractTransactionalJUnit4SpringContextTests (yes, wow what a long class name). For those of you unfamiliar with this class, basically if you extend your unit test class from this class, every
@Test annotated test method will run within a transaction, with the default behavior being that a rollback is called after each
@Test method’s execution (unless you declare so otherwise). Pretty convenient.
My simple test case was leveraging a DAO that derived from
HibernateDaoSupport and the test inserted a few records. When the test was complete, another method annotated with
@AfterTransaction would verify that the data did NOT exist in the table, which was expected due to the rollback that occurs after each test case…… well my asserts were failing because Hibernate created the MySQL tables using MyISAM, which does not support transactions. If you encounter this kind of issue, all you have to do is change your Hibernate dialect to use the
MySQL5InnoDBDialect (for InnoDB storage which does transactions) within your session factory configuration as such:
... <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> ...