I want to test hibernate session's save() method using spring testing framework. @Test method is :
@Test
@Transactional
public void testSave() {
User expected = createUser();
getGenericDao().currentSession().save(expected);
User actual = getUser(generatedId);
assertUsersEqual(expected,actual);
}
I want to flush user into database. I want my user to be in database after this method
getGenericDao().currentSession().save(expected);
Then I want to go to database using spring data framework and fetch this saved user by next line:
User actual = getUser(generatedId);
I tried to use hibernate flush method like:
currentSession().setFlushMode(MANUAL);
//do saving here
currentSession().flush();
It does not flush my user into database! However if I don't make use of @Transactional spring annotation and save my user in programmatic spring transaction I achieve what I want. Unfortunately then user saved into db is not rollbacked as there is no spring @Transactional. Therefore my test method changes the db and behaviour of subsequent test methods.
So I need to flush my user into db inside test method(not at the end) and at the end of test method rollback all changes to db.
UPDATE suggestion to prepare method as follows:
@Transactional
public void doSave(User user){
getGenericDao().currentSession().save(user);
}
And call doSave inside testSave is doing nothing. Still I have no user in db after executing this method. I set breakpoint and check my db from command-line.
UPDATE Thanks very much for response. The problem is that method flush() does not put my user into database.I tried Isolation.READ_UNCOMMITTED and it does not put my user into database. I can achieve what I want but only if I turn off spring transaction on @Test method and do saving in programmatic transaction. BUT then @Test method is not rolled back leaving saved user for subsequent @Test methods. Here @Test method to save user is not as dangerous as @Test method to delete user, because it is not rolled back. So there must spring transactional support for @Test method with which I can't anyway put my user(or delete) into db. Actually user is put(or deleted) into db only after @Test method ends and transaction for @Test method is comitted. So I want to save my User into db in the middle of @Test method and roll back it at the end of @Test method
Thank you!
Finally I stuck to the following solution:
First, my @Test
methods are not running within spring @Transactional
support. See this article to know how dangerous it may be. Next, instead of using @Repository
beans inside @Test
methods I autowire @Service
beans which use @Transactional
annotation.
The miracle is that @Test
method like this
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void testSave() {
Answer created = createAnswer();
Long generatedId = answerService.save(created);
//at this moment answer is already in db
Answer actual=getAnswerById(generatedId);
... }
puts my Answer object into database (just after answerService.save(created);
) and method getAnswerById
goes to DB and extracts it to check if save was correct.
To eliminate changes made to database in @Test
method I recreate database by JdbcTestUtils.executeSqlScript