The method execute has void as a return type, instead of a String.
2016-02-13 2:26 GMT-05:00 小唐 <[email protected]>:
> Hi,
>
> I was trying to use StrutsTestCase to write a test for a Bean, which has a
> member of String[]. The code runs perfectly in servlet container(Tomcat),
> but fails as a mock test. In EditActionTest.java, assertNotNull("The action
> is null but should not be.", action) still works fine, but String result =
> actionProxy.execute() returns the result as "error" instead of "success".
> All the related codes are listed below.
>
> Can anyone give me a hint?
> Thanks in advance! Tom
>
> Person.java
> public class Person{
> String[] models = new String[]{"Ford", "Nissan"};
>
> public void setModels(String[] models){
> this.models = models;
> }
>
> public String[] getModels(){
> return models;
> }
> }
>
> Edit.java
> public class EditAction extends ActionSupport{
> Person personBean;
>
> public void execute(){
> return SUCCESS;
> }
> }
>
> struts.xml
> <struts>
> <action name="edit" class="EditAction" method="execute">
> <result name="input">/edit.jsp</result>
> <result name="success">/thankyou.jsp</result>
> </action>
> </struts>
>
> edit.jsp
> <%@ taglib prefix="s" uri="/struts-tags" %>
> <s:form action="save" method="post">
> <s:checkboxlist key="personBean.models" list="Ford, Nissan" />
> <s:submit key="submit" />
> </s:form>
>
> EditActionTest.java
> public class EditActionTest extends StrutsTestCase {
> @Test
> public void testExecuteValidationPasses() throws Exception {
> request.setParameter("personBean.models", new String[]{"Ford",
> "Nissan"});
> ActionProxy actionProxy = getActionProxy("/edit.action") ;
> EditAction action = (EditAction) actionProxy.getAction();
> assertNotNull("The action is null but should not be.", action);
> String result = actionProxy.execute();
> assertEquals("The execute method did not return " +
> ActionSupport.SUCCESS + " but should have.", ActionSupport.SUCCESS, result);
> }
> }