1

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

8
  • try adding @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) in the test class level. by line 3 data should be available to query from DB Commented Jul 13, 2013 at 13:49
  • I tried to add this annotation, but it still doesn't work. Keep committing after the function ends Commented Jul 13, 2013 at 13:58
  • by line 3 data should be available to query from persisted object , Way of verifying is dao.addPerson(newPerson); int testId = newPerson.getAppId();Assert.assertEquals(1, testId); Commented Jul 13, 2013 at 13:59
  • Reason is, Hibernate keep attach the object to session which is in persisted state, so any query to the object will return you the id assigned by auto increment strategy you have used Commented Jul 13, 2013 at 14:02
  • 1
    Here you go: Please follow this blog entry which has a sample junit test cases for save and delete scenario. Please see the section 6 for more details. In simple word, when you are deleting a object, immediate below line load/find the object and check for assertNull blog.paxcel.net/blog/… Commented Jul 13, 2013 at 14:38

1 Answer 1

1

In order to test the insertion, you can use the following assertion:

Assert.assertNotNull(dao.findById(newPerson.getId()));

For deletion check, you can try the following test:

@Test
public void testDelete(){
    ...
    dao.removePerson(person);
    Assert.assertNull(dao.findById(person.getId()));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.