整合Spring和Hibernate之后,对于业务逻辑类的测试方法,可让测试类继承AbstractTransactionalSpringContextTests,
而不是直接继承TestCase,避免在测试单元中出现懒加载问题。
- 重点理解为何会出现懒加载问题
package com.bjsxt.oa.web.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.bjsxt.oa.manager.OrgManager;
public class OrgAction extends DispatchAction {
private OrgManager orgManager;
/**
* 进入主界面
*/
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO 查询机构列表
return mapping.findForward("index");
}
/**
* 打开添加界面
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward addInput(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("add_input");
}
//添加机构信息
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("add_success");
}
public ActionForward del(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("del_success");
}
public ActionForward updateInput(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("update_input");
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("update_success");
}
public void setOrgManager(OrgManager orgManager) {
this.orgManager = orgManager;
}
}
package com.bjsxt.oa.web.forms;
import org.apache.struts.action.ActionForm;
public class OrgActionForm extends ActionForm {
private int id;
private String name;
private String sn;
private String description;
private int parentId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean name="/org" class="com.bjsxt.oa.web.actions.OrgAction" scope="prototype">
<property name="orgManager" ref="orgManager"/>
</bean>
</beans>
package com.bjsxt.oa.manager;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.AbstractTransactionalSpringContextTests;
import com.bjsxt.oa.model.Organization;
public class OrgManagerTest extends AbstractTransactionalSpringContextTests {
private OrgManager orgManager;
@Override
protected String[] getConfigLocations() {
return new String[]{"applicationContext-*.xml"};
}
public void testAddOrg() {
// Organization org = new Organization();
// org.setName("测试机构");
// org.setDescription("描述");
// om.addOrg(org, 0);
for(int i=0; i<5; i++){
Organization org = new Organization();
org.setName("父机构"+i);
orgManager.addOrg(org, 0);
for(int j=0; j<10; j++){
Organization c = new Organization();
c.setName("["+org.getName()+"]下面的子机构"+j);
orgManager.addOrg(c, org.getId());
}
}
//使得父类能够提交事务
setComplete();
}
public void testDelOrg() {
fail("Not yet implemented");
}
public void testUpdateOrg() {
fail("Not yet implemented");
}
public void testFindOrg() {
Organization org = orgManager.findOrg(5);
System.out.println(org.getName());
}
public void testSearchOrgs() {
List orgs = orgManager.searchOrgs(0);
for (Iterator iterator = orgs.iterator(); iterator.hasNext();) {
Organization org = (Organization) iterator.next();
System.out.println(org.getName());
}
}
public void setOrgManager(OrgManager orgManager) {
this.orgManager = orgManager;
}
}