Spring+hibernate+junit's DAO Test

Source: Internet
Author: User
Tags log log

Develop a eclipse3.4+struts2.0+hibernate3.2+spring2.5+oracle10g project. After implementing the DAO layer, you want to test the DAO layer. Used to be used Junit3.8, this time for the latest Junit4.4, but in writing test class encountered a lot of trouble, will encounter problems and solutions recorded, for later friends reference.


The following sections explain how to write test classes under Junit4.4 and Junit3.8.
For Junit3.8:
Spring's test of the DAO layer, There is a abstracttransactionaldatasourcespringcontexttests class in the Spring-test.jar package, as long as you inherit it, and then rewrite the Getconfiglocations method. It is relatively simple, and it supports rollback, without the time-consuming operation of the database.
You don't have to move for all of your applicationcontext.xml, such as the spring configuration file. The default classpath path for Eclipse is build/classes under the project path, and be careful not to be overwritten by your ant-written build, and the ant build does not compile the test package. The test class was eventually not found.


Let's write a basic test class first, and then just inherit the class and add the test method.

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

Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;


public class BaseDaoTestCaseJunit3 extends
abstracttransactionaldatasourcespringcontexttests {

Protected final static log log = Logfactory.getlog (Basedaotestcasejunit3.class);

@Override
Protected string[] Getconfiglocations () {
This.setautowiremode (Autowire_by_name);
return new string[]{"Classpath:/applicationcontext.xml", "Classpath:/spring-config-*.xml"};
}

public void Testconfig () {
Assertnotnull ("Spring-mock Context has bean init ()", this.applicationcontext);
}
}

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

The following class inherits from the above BaseDaoTestCaseJunit3 and only tests the Findbyemail method in the next Custinfodao.
Import Com.psi.domain.CustInfo;


public class CustInfoDaoTests3 extends BaseDaoTestCaseJunit3 {
Private Custinfodao Cidao;
Private CustInfo CI;

Public CustInfoDaoTests3 () {}

public void Setcustinfodao (Custinfodao Cidao) {
This.cidao = Cidao;
}


public void GetList () {
try{
CI = Cidao.findbyemail ("sue76543@ms5.url.com.tw"); This method returns the object directly.
Assertnotnull (Ci.getid ());
}catch (Exception e) {
E.printstacktrace ();
}
}
}

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

Here is the Junit4.4:
JUNIT4 is completely different from the JUNIT3, its configuration files and so on only need to be marked in annotation. TransactionManager is the TransactionManager you configure in the application.

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

Package Com.psi.dao;


Import Org.junit.runner.RunWith;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Import org.springframework.test.context.transaction.TransactionConfiguration;
Import org.springframework.transaction.annotation.Transactional;


@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"Classpath:/applicationcontext.xml", "Classpath:/spring-config-*.xml"})
@TransactionConfiguration (transactionmanager= "TransactionManager", Defaultrollback=true)
@Transactional
public class BaseDAOTestCaseJunit44 {
This class as a base class, all the things to be used are configured in the annotation. So you don't have to write any content.
}

--------------------------------------------------------------------------------
The DAO test class inherits from the base class above and adds a test method, Findbyemail.


Import static org.junit.assert.*;


Import Org.junit.Before;
Import Org.junit.Test;
Import org.springframework.beans.factory.annotation.Autowired;


Import Com.psi.domain.CustInfo;


public class Custinfodaotests extends BaseDAOTestCaseJunit44 {
Private Custinfodao Cidao;
Private CustInfo CI;

Public custinfodaotests () {}

@Autowired
public void Setcustinfodao (Custinfodao Cidao) {
This.cidao = Cidao;
}

@Before
public void init () {}

@Test
public void GetList () {
try{
CI = Cidao.findbyemail ("sue76543@ms5.url.com.tw");
Assertnotnull (Ci.getid ());
}catch (Exception e) {
E.printstacktrace ();
}
}
}

--------------------------------------------------------------------------------
is as simple as this, but be aware that you want to load the appropriate package. Spring-test.jar, Apache's commons-pack is best to add. (You can basically use it in the project) The
also needs to be aware of your configuration file path.
I have also encountered the following error:
caused by:java.lang.NullPointerException
    at Org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor (logfactoryimpl.java:374)
This is the Eclipse bug, It does not automatically load all the packages under the Systemlibrary in the test environment, so you need to change your library to user-library.

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.