Write unit tests for DAO components in Java EE

Source: Internet
Author: User
Tags prepare

Unit testing as a guarantee of software quality and reconstruction of the foundation, has long been recognized by the broad masses of developers. Unit testing is a fine-grained test in which more and more developers submit unit tests while submitting functional modules. For most developers, writing unit tests has become a necessary process and best practice in the development process.

It is easy to write unit tests for common logical components, because logical components usually only require memory resources, so setting the input and output can write valid unit tests. For slightly more complex components, such as servlet, we can write our own mock objects to simulate objects such as HttpRequest and HttpResponse, or, using a dynamic simulation library such as Easymock, we can implement the corresponding mock objects on any interface, This results in effective unit testing of the components that are dependent on the interface.

In Java EE Development, writing Unit tests on DAO components is often a very complex task. Unlike other components, DAO components often rely on the underlying database, as well as the JDBC interface or an ORM framework (such as Hibernate), where the testing of DAO components often requires the introduction of transactions, which increases the complexity of writing unit tests. Although the use of Easymock can also simulate any JDBC interface object, or the main interface of ORM Framework, but its complexity is often very high, need to write a large number of analog code, and code reusability is very low, or even directly in the real database environment test. However, there is also an obvious disadvantage of using the real database environment, we need to prepare the database environment, prepare the initial data, and each time the unit test run, its database existing data will directly affect the next test, it is difficult to achieve "run, run repeatedly" unit test good practice.

In this paper, a more appropriate writing strategy for unit tests is given for DAO components. In the development of Java EE Development Network, in order to carry on the effective unit test to the DAO component, we use HSQLDB this small pure Java database as the test period database environment, with Ant, realizes the automatic generation database script, the test automatic initialization database, Greatly simplifies the writing of unit tests for DAO components.

In the Java realm, JUnit, as the first unit test framework, has been the most widely used and arguably the standard framework for Java Domain Unit testing. This article takes the latest JUnit version 4 as an example to demonstrate how to create a unit test case for a DAO component.

The Javaeedev persistence layer uses Hibernate 3.2, and the underlying database is MySQL. To demonstrate how to unit test DAO, we simplified it to a daotest project:

Because the transaction of hibernate is bound to thread, the Hibernateutil class is responsible for initializing sessionfactory and getting the current session:

  public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration()
.configure()
.buildSessionFactory();
}
catch(Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
}
HibernateUtil还包含了一些辅助方法,如:  public static Object query(Class clazz, Serializable id);
public static void createEntity(Object entity);
public static Object queryForObject(String hql, Object[] params);
public static List queryForList(String hql, Object[] params);

There is no more to be said here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.