基于JUnit3.8的测试模板类: package org.niit.sshpopedom.utest; import javax.annotation.Resource; import org.niit.sshpopedom.biz.IDepartBiz; import org.niit.sshpopedom.biz.IEmpBiz; import org.niit.sshpopedom.biz.impl.DepartBiz; import org.niit.sshpopedom.biz.impl.EmpBiz; import org.springframework.test.AbstractTransactionalSpringContextTests; import org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests; public class SpringTestTemplate extends AbstractTransactionalSpringContextTests{ protected IDepartBiz departBiz; protected IEmpBiz empBiz; public void setDepartBiz(IDepartBiz departBiz) { this.departBiz = departBiz; } public void setEmpBiz(IEmpBiz empBiz) { this.empBiz = empBiz; } @Override protected String[] getConfigLocations() { return new String[]{ "classpath:org/niit/sshpopedom/config/spring-datasource.xml", "classpath:org/niit/sshpopedom/config/spring-transaction.xml" }; } } 基于JUnit4的测试模板类: package org.niit.sshpopedom.utest; import javax.annotation.Resource; import org.junit.runner.RunWith; import org.niit.sshpopedom.biz.IDepartBiz; import org.niit.sshpopedom.biz.IEmpBiz; import org.niit.sshpopedom.biz.impl.DepartBiz; import org.niit.sshpopedom.biz.impl.EmpBiz; import org.springframework.test.AbstractTransactionalSpringContextTests; import org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests; 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:org/niit/sshpopedom/config/spring-datasource.xml","classpath:org/niit/sshpopedom/config/spring-transaction.xml"}) @TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false) @Transactional public class SpringJUnit4TestTemplate{ @Resource(name="departBiz") protected IDepartBiz departBiz; @Resource(name="empBiz") protected IEmpBiz empBiz; public void setDepartBiz(IDepartBiz departBiz) { this.departBiz = departBiz; } public void setEmpBiz(IEmpBiz empBiz) { this.empBiz = empBiz; } } 测试子类[对部门业务对象进行测试]: package org.niit.sshpopedom.utest; import java.util.Calendar; import org.apache.log4j.Logger; import org.junit.Test; import org.niit.sshpopedom.entities.Depart; public class DepartBizTest extends SpringTestTemplate { public void testGetDeparts(){ this.logger.info("start test getDeparts..........."); for(Depart d: departBiz.getDeparts()){ logger.info(d.getDid()+"/t"+d.getDname()); } this.logger.info("end test getDeparts..........."); } public void testAddDeparts(){ super.logger.info("start test addDepart..........."); departBiz.addDepart( new Depart("技术部17", "技术部17", Calendar.getInstance().getTime(),null), new Depart("技术部18", "技术部18", Calendar.getInstance().getTime(),null), new Depart("技术部19", "技术部19", Calendar.getInstance().getTime(),null) ); super.logger.info("end test addDepart..........."); } } 测试子类[对员工业务对象进行测试]: package org.niit.sshpopedom.utest; import org.junit.Test; import org.niit.sshpopedom.entities.Depart; import org.niit.sshpopedom.entities.Emp; import org.springframework.test.annotation.Rollback; public class EmpBizTest extends SpringJUnit4TestTemplate { @Test @Rollback(false) public void testAddEmps(){ Depart d1 = new Depart(); d1.setDid(22); Emp e1 = new Emp(); e1.setDepart(d1); e1.setEaddr("成都市锦江区"); e1.setEname("白菜"); e1.setEsex("女"); e1.setEtel("123"); Emp e2 = new Emp(); e2.setDepart(d1); e2.setEaddr("成都市锦江区"); e2.setEname("萝卜"); e2.setEsex("男"); e2.setEtel("321"); empBiz.addEmps(e1,e2); } } 关于注解含义参见spring2.5文档