使用junit 测试 , struts2测试,hibernate测试,等各环节的测试 SSH框架

被测试的类

package org.virus.struts.action; import java.util.ArrayList; import java.util.List; import org.virus.hibernate.ProbeManager; import org.virus.model.PagerModel; import org.virus.model.Probe; import org.virus.struts.BaseAction; import com.opensymphony.xwork2.ModelDriven; public class ProbeAction extends BaseAction implements ModelDriven<Probe> { private static final long serialVersionUID = 8991072579599050404L; private ProbeManager probeManager; private Probe model = new Probe(); private String idArr; private String column; private String keyword; private List<?> resutls = new ArrayList<Probe>(); private int result; @Override public String execute() throws Exception { PagerModel pm = new PagerModel(); pm = probeManager.find(currentPage, pageSize); resutls = pm.getList(); return SUCCESS; } public String likeSearch() throws Exception { PagerModel pm = new PagerModel(); pm = probeManager.likeSearch(column, keyword, currentPage, pageSize); resutls = pm.getList(); return SUCCESS; } public String likeSearchCount() throws Exception { result = probeManager.likeSearchCount(column, keyword); return SUCCESS; } public String deletes() throws Exception { probeManager.deletes(idArr); return SUCCESS; } public String editor() throws Exception { probeManager.saveOrUpdate(model); return SUCCESS; } /* ######################GET SET######################### */ public void setProbeManager(ProbeManager probeManager) { this.probeManager = probeManager; } public int getResult() { return result; } public void setResult(int result) { this.result = result; } public Probe getModel() { return model; } public void setModel(Probe model) { this.model = model; } public List<?> getResutls() { return resutls; } public void setResutls(List<?> resutls) { this.resutls = resutls; } public void setIdArr(String idArr) { this.idArr = idArr; } public void setColumn(String column) { this.column = column; } public void setKeyword(String keyword) { this.keyword = keyword; } }

 

测试中涉及dao注入,所以封闭了一个方法

注入spring的配置文件

package org.virus.struts.action; import junit.framework.TestCase; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ActionTestBaseCase extends TestCase { private ApplicationContext context = null; protected final static Log log = LogFactory .getLog(ActionTestBaseCase.class); protected void setUp() throws Exception { super.setUp(); context = new ClassPathXmlApplicationContext(new String[] { "classpath:/config/spring/applicationContext.xml", "classpath:/config/spring/applicationContext-hibernate.xml" }); } public ApplicationContext getContext() { return context; } public void testContext() { assertNotNull("struts context has bean init ", this.getContext()); } }

 

最终测试ProbeAction的类

package org.virus.struts.action; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.virus.hibernate.ProbeManager; /** * ProbeAction测试 * @author Administrator * */ public class ProbeActionTest extends ActionTestBaseCase{ private ApplicationContext context; private ProbeAction probeAction; private ProbeManager probeManager; /**/ public void setUp() throws Exception { super.setUp(); context = getContext(); probeAction = new ProbeAction(); probeManager = (ProbeManager)context.getBean("probeManager"); probeAction.setProbeManager(probeManager); } /*测试默认的action*/ public void testDefaultAction() throws Exception { probeAction.setCurrentPage(0); probeAction.setPageSize(Integer.MAX_VALUE); String result = probeAction.execute(); List<?> list = probeAction.getResutls(); System.out.println(list.size()); assertEquals("success",result); } /*测试模糊查询*/ public void testLikeSearch() throws Exception { probeAction.setCurrentPage(0); probeAction.setPageSize(Integer.MAX_VALUE); probeAction.setColumn("nameOfPrimer,virusName"); probeAction.setKeyword("a"); String result = probeAction.likeSearch(); List<?> list = probeAction.getResutls(); assertEquals("success",result); assertTrue(list.size()>0); } @Autowired public void setProbeManager(ProbeManager probeManager) { this.probeManager = probeManager; } }

 

struts官网的两篇测试说明

http://struts.apache.org/2.2.1.1/docs/struts-2-junit-plugin-tutorial.html

http://struts.apache.org/2.2.1.1/docs/testing-actions.html

 

你可能感兴趣的:(Hibernate,exception,struts,测试,JUnit,ssh)