Junit For Hibernate Enviornment
Hi,
i want to write Junit for Hibernate Enviornment ,Anyhelp ,Like a Complete Example including Basic(Class to be tested ) and Test class and any other file needed Be Made Available. For Any help Accept Thanks in Advance.
This might be enough for you to get started:
You have a database table called PERSON and a java class called Person that represents one record in that table.
You have the following basic CRUD functions created in Hibernate:
public Person getPerson(int ssn);
public boolean deletePerson(int ssn);
public boolean insertPerson(Person person)
public boolean updatePerson(Person person)
Ideally, create a JUNIT function that tests each function in isolation, using session.open and session.close per examples in your hibernate book.
There is no need to open or close any connections (or create a connection pool) in JUNIT since that is done automatically by hibernate and hidden from you.
JUNIT Example (doesnt compile, has bugs, but you get the idea):
/** test insertPerson */
public void testInsertPerson(){
Session session=null;
try{
session=HibernateUtil.getSessionFactory().openSession();
Person person=new Person('123-45-6789");
boolean personInserted= personDAO.insertPerson(person);
assertTrue(personInserted,"person was not inserted");
//cleanup test
deletePerson("123-45-6789");
} catch(Exception e) {
fail("exception was thrown", e);
} finally {
if(session!=null && session.isOpen())
session.close();
}
I suggest using JUNIT3 and not JUNIT4 since its easier to create.
Problems with using JUNIT testing:
1) You probably need to test your CRUD operations all in one function because you cant isolate them in individual functions Example: to test getPerson, you have to insertPerson first, then cleanup by calling deletePerson.
2) Make sure you dont overwrite good data in your test database during testing, and make sure you delete any test data from the test database when done testing. You may also put code in your setup() function to throw an exception rather than run, if the tests try to run against anything other than the test database (such as the production database).