I was wondering if you can help me out. I've just made a new project with spring, hibernate, mysql (innodb), etc. and I'm trying to get my first test cases working against a very simple dao and very simple test class.
Basically, the transaction is getting rolled back according to the logger each time. In the delete test, the transaction is actually rolled back in the database. With the store, the transaction is not rolled back and there is data left over from the test in the database. This is undesirable. Besides using DBUnit, is there an easy way to fix this?
Here is my code:
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "file:./war/WEB-INF/applicationContext-local.xml" } )
@TransactionConfiguration( transactionManager = "transactionManager" )
@Transactional
public class UserDaoImplTest {
@Autowired
private UserDao userDao;
@Test
public void testStoreUserWhenNew() {
User user = new User( "Bob", "Johnson" );
userDao.store( user );
Assert.assertNotNull( user.getId() );
}
@Test
public void testDeleteUser() {
userDao.delete( 1 );
Assert.assertNull( userDao.find( 1 ) );
}
@Test
public void testFindUser() {
User user = userDao.find( 1 );
Assert.assertEquals( "Mystic", user.getFirstName() );
Assert.assertEquals( "Rules", user.getLastName() );
}
@Test
public void testFindByFirstName() {
Collection collection = userDao.findByFirstName( "Mystic" );
Assert.assertEquals( collection.size(), 1 );
}
@Test
public void testFindByLastName() {
Collection collection = userDao.findByLastName( "Rules" );
Assert.assertEquals( collection.size(), 1 );
}
}


Reply With Quote

