也来说说struts2的测试
jiyanliang
2008-10-12
自己使用struts2也有一段时间了,对于struts2的测试却很少使用。
对于只使用struts2的mvc,进行单元测试应该没有问题。 但是在struts2的action中注入大量的service和定义全局的变量之后,测试action并不是太好做。大家有什么好的方法呢? |
|
zhuzhsh
2008-10-12
跟struts2+spring结合测试就方便了!
|
|
jiyanliang
2008-10-12
zhuzhsh 写道 跟struts2+spring结合测试就方便了!
多谢指点 |
|
kyo100900
2008-10-13
看看我很早这前写的例子吧:
GroupAction是我要测试的Action,测试里面的各个方法。 public class GroupActionTest extends BaseActionTest { private ApplicationContext context = null; private GroupService groupService = null; private ConferenceMessageService conferenceMessageService = null; private GroupAction groupAction = null; private SignUpPersonService signUpPersonService = null; private GroupPersonService groupPersonService = null; private WordService wordService = null; private MeetingTypeService meetingTypeService = null; private ConferenceService conferenceService = null; private TemplateService templateService = null; private static String conferenceId = "8a29292516ec0caf0116ec0db9920002"; private static String id = ""; private static String[] ids = { "1", "2" }; protected void setUp() throws Exception { super.setUp(); context = getApplicationContext(); groupService = (GroupService) context.getBean("groupService"); conferenceMessageService = (ConferenceMessageService) context .getBean("noteSignupService"); signUpPersonService = (SignUpPersonService) context .getBean("signUpPersonService"); groupPersonService = (GroupPersonService) context .getBean("groupPersonService"); wordService = (WordService) context.getBean("wordService"); meetingTypeService = (MeetingTypeService) context .getBean("meetingTypeService"); conferenceService = (ConferenceService) context .getBean("conferenceService"); templateService = (TemplateService)context.getBean("templateService"); } public void testList() { groupAction = new GroupAction(); groupAction.setConfId(conferenceId); groupAction.setGroupService(groupService); String result = groupAction.list(); int listSize = groupAction.getPaginationList().getRecordList().size(); assertEquals(2, listSize); assertEquals("success", result); } public void testSave() { groupAction = new GroupAction(); groupAction.setGroupService(groupService); String result = groupAction.save(); assertEquals("success", result); } public void testSaveByGroupPerson() { Group groupProxy = new Group(); groupProxy.setCaller("1"); groupProxy.setConferenceId(conferenceId); groupProxy.setLinkman("1"); groupProxy.setName(Integer.parseInt("1")); groupProxy.setOrgList("1"); groupProxy.setPhone("1"); groupProxy.setPlace("1"); groupProxy.setOrgListIds("8a2929ba15fa12cf0115fa285c840002"); groupAction = new GroupAction(); groupAction.setGroup(groupProxy); groupAction.setGroupService(groupService); groupAction.setConferenceMessageService(conferenceMessageService); groupAction.setConfId(conferenceId); String result = groupAction.save(); assertEquals("success", result); } public void testDelete() { groupAction = new GroupAction(); groupAction.setIds(ids); groupAction.setGroupService(groupService); String result = groupAction.delete(); assertEquals("success", result); } public void testGroupAdjust() { groupAction = new GroupAction(); groupAction.setConfId(conferenceId); groupAction.setGroupService(groupService); groupAction.setConferenceMessageService(conferenceMessageService); groupAction.setSignUpPersonService(signUpPersonService); groupAction.setGroupPersonService(groupPersonService); String result = groupAction.groupAdjust(); assertEquals("success", result); List persons = groupAction.getUnSignUpPersons(); // assertEquals(3, persons.size()); for (int i = 0; i < persons.size(); i++) { SignUpPerson p = (SignUpPerson) persons.get(i); System.out.println("Name is : " + p.getName() + ", " + p.getConferenceMessage().getOrg()); } } public void testChangeGroup() throws Exception { Group groupProxy = this.groupService .getGroupByID("8a29292516b3f45d0116b3f529d60001"); groupAction = new GroupAction(); groupAction.setConfId(conferenceId); groupAction.setGroupService(groupService); groupAction.setGroup(groupProxy); groupAction.setConferenceMessageService(conferenceMessageService); groupAction.setSignUpPersonService(signUpPersonService); groupAction.setGroupPersonService(groupPersonService); String result = groupAction.changeGroup(); assertEquals("success", result); } public void testSelectGroupOrgs() { groupAction = new GroupAction(); groupAction.setConfId(conferenceId); groupAction.setGroupService(groupService); groupAction.setConferenceMessageService(conferenceMessageService); groupAction.setSignUpPersonService(signUpPersonService); groupAction.setGroupPersonService(groupPersonService); String result = groupAction.selectGroupOrgs(); assertEquals("success", result); List persons = groupAction.getConferenceMessages(); // assertEquals(3, persons.size()); for (int i = 0; i < persons.size(); i++) { ConferenceMessage p = (ConferenceMessage) persons.get(i); System.out.println("Name is : " + p.getId() + ", " + p.getOrg()); } } public void testPrepareword() { groupAction = new GroupAction(); groupAction.setConfId(conferenceId); groupAction.setGroupService(groupService); groupAction.setConferenceMessageService(conferenceMessageService); groupAction.setSignUpPersonService(signUpPersonService); groupAction.setGroupPersonService(groupPersonService); groupAction.setWordService(wordService); groupAction.setMeetingTypeService(meetingTypeService); groupAction.setTemplateService(templateService); Conference conf = conferenceService.getObject(conferenceId); groupAction.setConference(conf); groupAction.prepareword(); System.out.println("Name is : " + groupAction.getWordid()); } |
|
jiyanliang
2008-10-14
非常感谢你的分享。
之前对测试一直不够重视,现在突然间又感觉到测试的重要了。 |
|
xredleaf
2008-11-12
BaseActionTest 这个类是哪个包下面的,能说一下吗?
|
|
kyo100900
2008-11-12
import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BaseActionTest extends TestCase { private ApplicationContext context = null; protected void setUp() throws Exception { super.setUp(); context = new ClassPathXmlApplicationContext(new String[] { "configuration/applicationContext-Action.xml", "configuration/context.xml" }); } public ApplicationContext getApplicationContext() { return context; } }
|