我有以下的Struts Action:單元測試Struts的
public ActionForward addSomething(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
SomeForm sForm = (SomeForm) form;
Test t = testForm.toTest();
if (tDao.checkExistsTest(t.getTest())) {
return mapping.findForward("failure");
} else {
t.setType(new TestType(tess));
t.setPassword(testForm.getPassword());
tDao.add(t);
return mapping.findForward("success");
}
}
我做了以下代碼來測試testAddSomethingSuccess方法:
@Test
public void testAddSomethingSuccess() throws Exception {
form.setTest("LOL");
form.setTestName("lol");
form.setPassword("12345");
ActionForward forward = action.addSomething(mapping, form, request, response);
assertEquals(tDao.getList().get(0).getTest(), "LOL");
assertEquals(tDao.getList().get(0).getTestName(), "lol");
assertEquals(tDao.getList().get(0).getPassword(), "12345");
assertEquals("success", forward.getName());
}
我如何能實現testAddClientFailed()? ?? :
@Test
public void testAddSomethingFailed() throws Exception {
form.setTestName("lol");
t.checkIfExists("lol");
ActionForward forward = action.addSomething(mapping, form, request, response);
assertEquals("failure", forward.getName());
}
嗯,我做了你所說的,但我仍然無法進行測試。我不知道爲什麼:s – unnamed
你需要告訴我們你做了什麼。 – blank
我編輯我的問題添加:tDao.checkIfExists(「lol」); – unnamed