我有以下 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());
}