s2+s2+h3 中的测试类
在struts2+spring2+hibernate3中的测试类
把主要测试文档贴出来
一。。所有的测试集合
importjunit.framework.Test;
importjunit.framework.TestSuite;
publicclassAllTests{
publicstaticTestsuite(){
TestSuitesuite=newTestSuite(
"Testforcom.serviceImpl.orderServiceTest");
//$JUnit-BEGIN$
//orderServiceTest
suite.addTestSuite(OrderPubServiceImplTest.class);
suite.addTestSuite(FinaImplTest.class);
suite.addTestSuite(OrderLoadServiceImplTest.class);
suite.addTestSuite(SendGoodsImplTest.class);
suite.addTestSuite(StafferImplTest.class);
//stockServiceTest
suite.addTestSuite(StockBuyServiceImplTest.class);
suite.addTestSuite(StockFinaServiceImplTest.class);
suite.addTestSuite(StockStorServiceImplTest.class);
suite.addTestSuite(StockViewServiceTest.class);
//$JUnit-END$
returnsuite;
}
}
二。。使用spring的AbstractTransactionalDataSourceSpringContextTests,每个业务测试类都集成此类,测试共用的方法也可以放在里面
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importorg.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
importcom.opensymphony.xwork2.ActionContext;
importcom.entityUserDefined.Orderbean;
publicclassJunitTestextends
AbstractTransactionalDataSourceSpringContextTests{
publicString[]getConfigLocations(){
setAutowireMode(AUTOWIRE_BY_TYPE);
setDefaultRollback(true);
returnnewString[]{"classpath:applicationContext*.xml"};
}
}
三。一个测试类。。。
importorg.junit.Test;
importtest.serviceImpl.AllTests.JunitTest;
importcom.service.OrderService.FinaOrderService;
publicclassFinaImplTestextendsJunitTest{
privateFinaOrderServicefinaImpl;
privateActionContextac=ActionContext.getContext();//模拟session使用
//模拟session时防止出现null值
protectedvoidonSetUp()throwsException{
if(ac.getSession()==null){
ac.setSession(newHashMap());
}
super.onSetUp();
}
//为业务接口注入
publicvoidsetFinaImpl(FinaOrderServicefinaImpl){
this.finaImpl=finaImpl;
}
//模拟session
publicvoidmockStaffInfo(){
StaffInfosi=newStaffInfo();
staffInfo.setName("测试junit");
staffInfo.setStaffId(6);
ac.getSession().put("staffInfo",staffInfo);
}
publicvoidtestSavfinishDate(){
mockStaffInfo();
assertEquals(true,finaImpl.sav("0001"););//测试业务逻辑方法sav
}
}