LookupDispatchAction et les tests unitaires
Bonjour,
je voudrais écrire des tets unitaires pour lookupdispatchaction mon action. cette action etends une.
Dans mes tests, je n'arrive pas à savoir commment setter l'operation(add, delete, etc) ce qui fait que j'ai l'erreur suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
ATTENTION: Unhandled exception
javax.servlet.ServletException: Action[/lookup] missing resource in key method map
at org.apache.struts.actions.LookupDispatchAction.getLookupMapName(LookupDispatchAction.java:232)
at org.apache.struts.actions.LookupDispatchAction.getMethodName(LookupDispatchAction.java:272)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:157)
at org.apache.struts.actions.LookupDispatchAction.execute(LookupDispatchAction.java:146)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:290)
at test.ListRecordsActionTest.addRecordsRecordHF(ListRecordsActionTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
16-oct.-2007 21:05:19 org.apache.struts.chain.commands.ExceptionCatcher postprocess |
Dans mon action, j'ai ceci comme operation
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public static final String OPERATION_AJOUTER = "operation.ajouter";
public static final String OPERATION_MODIFIER = "operation.modifier";
public static final String OPERATION_SUPPRIMER = "operation.supprimer";
public static final String OPERATION_TRIER = "operation.sort";
public Map getKeyMethodMap() {
Map<String, String> map = new HashMap<String, String>();
map.put(OPERATION_AJOUTER, "ajouter");
map.put(OPERATION_MODIFIER, "modifier");
map.put(OPERATION_SUPPRIMER, "supprimer");
map.put(OPERATION_TRIER, "trier");
return map;
} |
fk04
LookupDispatchAction et les tests unitaires
bonjour,
Mon problème est d'ecire un test unitaire permettant d'ajouter un element
genre ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public void addRecordsRecordHF() {
setRequestPathInfo("/lookup");
//ici je simule l'ajout des elements
addRequestParameter("firstname", "jojo");
addRequestParameter("lastname", "jiselle");
addRequestParameter("phone", "1234098");
addRequestParameter("departement", "5678");
//comment simuler l''action ajouter ?
actionPerform();
verifyNoActionErrors();
} |
Comment je peut setter dans ce test que je veux qu'il passe dans l'action ajouter ?
mon action ressemble à ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
public class LookupAction extends LookupDispatchAction {
private static Logger theLogger = Logger.getLogger(LookupAction.class.getName());
PhoneBook aPhoneBook = PhoneBook.getInstance();
public static final String OPERATION_AJOUTER = "operation.ajouter";
public static final String OPERATION_MODIFIER = "operation.modifier";
public static final String OPERATION_SUPPRIMER = "operation.supprimer";
public static final String OPERATION_TRIER = "operation.sort";
public Map getKeyMethodMap() {
Map<String, String> map = new HashMap<String, String>();
map.put(OPERATION_AJOUTER, "ajouter");
map.put(OPERATION_MODIFIER, "modifier");
map.put(OPERATION_SUPPRIMER, "supprimer");
map.put(OPERATION_TRIER, "trier");
return map;
}
public ActionForward ajouter(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException, RecordException {
ActionErrors errors = new ActionErrors();
LookupForm lookupForm = (LookupForm) form;
String lastname = lookupForm.getLastname();
String firstname = lookupForm.getFirstname();
String phone = lookupForm.getPhone();
String departement = lookupForm.getDepartement();
Employee employee = new Employee(new Personne(firstname, lastname),
phone, departement);
HashMap<String, Employee> map = new HashMap<String, Employee>();
if (request.getSession().getAttribute("map") != null) {
map = (HashMap<String, Employee>) request.getSession()
.getAttribute("map");
}
try{
aPhoneBook.addRecord(employee, employee.getPhone());
map = (HashMap<String, Employee>) aPhoneBook.getPhonebooklist();
}catch(PhoneBookException e){
errors.add("errorsaddreccord", new ActionMessage("errors.addrecord.keyfound"));
}
request.getSession().setAttribute("map", map);
lookupForm.doResetAddForm(mapping, request);
this.saveErrors(request, errors);
return (mapping.findForward("page"));
}
} |