I'm writing a unit test for a DAO call to insert a record into the DB using Hibernate.
It's transactional and I used @Transactional in that way:
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations={"/spring-servlet.xml"})
@Transactional
public class MyDaoTest {
@Autowired
private MyDao dao;
@Test
@Rollback(false)
public void testAddEmployee() {
// Create some data (line 1)
dao.addPerson(newPerson); // (line 2)
// Query DB (line 3)
// Check result (line 4)
} // (line 5)
}
In the above code, the operation is committed only when the function terminates at line 5.
The fact is that at line 3 and 4 I need to query the DB to check if the instert has been completed succesfully. But I can't do that because my data is still not persisted.
What can I do in order to test this insert? Is there a way to commit before the end of the function?
I'm adding some more info here. This is the console output:
Hibernate: select person0_.ID as ID1_, person0_.NAME as NAME1_ from PERSON person0_
Hibernate: insert into PERSON (NAME) values (?, ?)
Hibernate: select person0_.ID as ID1_, person0_.NAME as NAME1_ from PERSON person0_
For testing I'm using JUnit