Loading
Unit Testing Hibernate

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  How to write an Unit Test Case for a Hibernate Class?


Ans)


Step 1:
Hibernate Classes also should be tested as normal classes, with exception of passing the
hibernate Properties files as follows.

System.setProperty("hibernate.properties.file.name", "hibernate_junit.properties");

Step 2:
In Hiberante Properties file or XML file you need to use dbcp Connection API as follows.

hibernate.dialect org.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.url jdbc:oracle:thin:@HostName:1522:DBName

hibernate.connection.username sa
hibernate.connection.password yourPassword
hibernate.connection.pool_size 5
hibernate.dbcp.poolPreparedStatements true
#hibernate.dbcp.ps.maxActive
hibernate.connection.provider_class com.salesorder.DBCPConnectionProvider

Step 3 :
Create Your own DBCPConnectionProvider as follows

public class DBCPConnectionProvider implements ConnectionProvider {

    private static final Log log = LogFactory.getLog(DBCPConnectionProvider.class);
    private static final String PREFIX = "hibernate.dbcp.";
    private BasicDataSource ds;
}

Step 4:
Write a TestCase as follows

public class SalesOrderTestCase extends TestCase {

    static {
        try {
            System.setProperty("hibernate.properties.file.name", "hibernateDEV_junit.properties");
            new HibernateConfig().init(null);
        }
        catch (Exception ex) {
            System.err.println("Could not initialize Hibernate Config");
            throw new RuntimeException("Could not initialize Hibernate Config");
        }
    }

    public SalesOrderTestCase() {
        super("SalesOrderTestCase");
    }

    public SalesOrderTestCase(String name) {
        super(name);
    }

    /**
     * override this in a subclass if you want to setup a separate principle than
     * nkuppili.
     */
    public void setUp() throws Exception {
     super.setUp();
    }

    public void tearDown() throws Exception {
        HibernateConfig.closeSession();
    }
}

 



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Unit Testing Hibernate


Unit Testing Hibernate

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free