You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by John Fitzpatrick <jo...@vitarara.net> on 2004/03/02 17:24:08 UTC

[REPOST] MockStrutsTestCase problem

Sorry for the repost, but I really am stuck on this. If anyone could help,
I'd appreciate it.



I'm having a problem setting up tests where errors are created during
validation by a DynaActionForm using MockStrutsTestCase. Here's my code:

The DynaActionForm:
===================
    package test;

    import javax.servlet.http.*;

    import org.apache.struts.action.*;

    public class BogusForm extends org.apache.struts.action.DynaActionForm {
        ActionErrors errors = new ActionErrors ();

       public ActionErrors validate ( ActionMapping mapping,
                HttpServletRequest request ) {
            
            errors.add ( "error", new ActionError ( "error.message" ) );
            return errors;
       }
    }

The Action:
===========
    package test;

    import javax.servlet.http.*;

    import org.apache.struts.action.*;

    /**
     * @struts.action
     *    name="BogusForm"
     *    input=".doActivity"
     *    path="/Bogus"
     * @struts.action-forward
     *    name="doActivity"
     *    path=".doActivity"
     */
    public class BogusAction extends org.apache.struts.action.Action {
       public ActionForward execute ( ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response )
            throws Exception {

          return mapping.findForward ( "doActivity" );
       }
    }

The MockStrutsTestCase:
=======================
    package test;

    import javax.servlet.*;
    import servletunit.struts.*;

    public class TestBogusAction extends MockStrutsTestCase {
       public void testFormPassed () {
          setRequestPathInfo ( "/Bogus" );
          actionPerform ();
          verifyNoActionErrors ();
       }
       public void testFormFailed () {
          setRequestPathInfo ( "/Bogus" );
          actionPerform ();
          verifyActionErrors( new String [] { "error.message" } );
       }
    }

The result:
===========
test:
    [junit] Running mori.web.controller.registry.test.TestBogusAction
    [junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 2.435 sec
    [junit] Testsuite: mori.web.controller.registry.test.TestBogusAction
    [junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 2.435 sec

    [junit] Testcase: testFormPassed took 1.875 sec
    [junit] Testcase: testFormFailed took 0.547 sec
    [junit]     FAILED
    [junit] was expecting some error messages, but received none.
    [junit] junit.framework.AssertionFailedError: was expecting some
error messages, but received none.
    [junit]     at 
servletunit.struts.Common.verifyActionMessages(Common.java:84)
    [junit]     at 
servletunit.struts.MockStrutsTestCase.verifyActionErrors(MockStrutsTestCase.
java:765)
    [junit]     at 
mori.web.controller.registry.test.TestBogusAction.testFormFailed(TestBogusAc
tion.java:17)
    [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
    [junit]     at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)
    [junit]     at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
    [junit] TEST mori.web.controller.registry.test.TestBogusAction FAILED



Since the Form just creates an error, I should get exactly the opposite test
results from what I'm getting, right? It seems as though the
MockStrutsTestCase cannot see the errors generated by the DynaActionForm
even though the source says it should do exactly that. Any help appreciated.

John



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


------ End of Forwarded Message


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: [REPOST] MockStrutsTestCase problem

Posted by John Fitzpatrick <jo...@vitarara.net>.
Sorry all,

Figured out the problem. Typo in the form definition <bangs head on desk>.
Thanks.

John



On 20040302 12:12 PM, "John Fitzpatrick" <jo...@vitarara.net> wrote:

> Richard,
> 
> Thanks for the pointer to DynaValidatorForm. I can see how that would be
> more useful than DynaActionForm for validation.
> 
> However, I tried making the change you suggest and I get the same result.
> Any other advice? It's as if the ActionErrors generated by the form are
> invisible to the test.
> 
> John
> 
> 
> On 20040302 11:37 AM, "Richard Yee" <ry...@yahoo.com> wrote:
> 
>> John,
>> In your form, you should extend
>> org.apache.struts.validator.DynaValidatorForm instead
>> of org.apache.struts.action.DynaActionForm
>> 
>> -Richard
>> 
>> 
>> --- John Fitzpatrick <jo...@vitarara.net> wrote:
>>> Sorry for the repost, but I really am stuck on this.
>>> If anyone could help,
>>> I'd appreciate it.
>>> 
>>> 
>>> 
>>> I'm having a problem setting up tests where errors
>>> are created during
>>> validation by a DynaActionForm using
>>> MockStrutsTestCase. Here's my code:
>>> 
>>> The DynaActionForm:
>>> ===================
>>>     package test;
>>> 
>>>     import javax.servlet.http.*;
>>> 
>>>     import org.apache.struts.action.*;
>>> 
>>>     public class BogusForm extends
>>> org.apache.struts.action.DynaActionForm {
>>>         ActionErrors errors = new ActionErrors ();
>>> 
>>>        public ActionErrors validate ( ActionMapping
>>> mapping,
>>>                 HttpServletRequest request ) {
>>>             
>>>             errors.add ( "error", new ActionError (
>>> "error.message" ) );
>>>             return errors;
>>>        }
>>>     }
>>> 
>>> The Action:
>>> ===========
>>>     package test;
>>> 
>>>     import javax.servlet.http.*;
>>> 
>>>     import org.apache.struts.action.*;
>>> 
>>>     /**
>>>      * @struts.action
>>>      *    name="BogusForm"
>>>      *    input=".doActivity"
>>>      *    path="/Bogus"
>>>      * @struts.action-forward
>>>      *    name="doActivity"
>>>      *    path=".doActivity"
>>>      */
>>>     public class BogusAction extends
>>> org.apache.struts.action.Action {
>>>        public ActionForward execute ( ActionMapping
>>> mapping,
>>>             ActionForm form, HttpServletRequest
>>> request,
>>>             HttpServletResponse response )
>>>             throws Exception {
>>> 
>>>           return mapping.findForward ( "doActivity"
>>> );
>>>        }
>>>     }
>>> 
>>> The MockStrutsTestCase:
>>> =======================
>>>     package test;
>>> 
>>>     import javax.servlet.*;
>>>     import servletunit.struts.*;
>>> 
>>>     public class TestBogusAction extends
>>> MockStrutsTestCase {
>>>        public void testFormPassed () {
>>>           setRequestPathInfo ( "/Bogus" );
>>>           actionPerform ();
>>>           verifyNoActionErrors ();
>>>        }
>>>        public void testFormFailed () {
>>>           setRequestPathInfo ( "/Bogus" );
>>>           actionPerform ();
>>>           verifyActionErrors( new String [] {
>>> "error.message" } );
>>>        }
>>>     }
>>> 
>>> The result:
>>> ===========
>>> test:
>>>     [junit] Running
>>> mori.web.controller.registry.test.TestBogusAction
>>>     [junit] Tests run: 2, Failures: 1, Errors: 0,
>>> Time elapsed: 2.435 sec
>>>     [junit] Testsuite:
>>> mori.web.controller.registry.test.TestBogusAction
>>>     [junit] Tests run: 2, Failures: 1, Errors: 0,
>>> Time elapsed: 2.435 sec
>>> 
>>>     [junit] Testcase: testFormPassed took 1.875 sec
>>>     [junit] Testcase: testFormFailed took 0.547 sec
>>>     [junit]     FAILED
>>>     [junit] was expecting some error messages, but
>>> received none.
>>>     [junit] junit.framework.AssertionFailedError:
>>> was expecting some
>>> error messages, but received none.
>>>     [junit]     at
>>> 
>> servletunit.struts.Common.verifyActionMessages(Common.java:84)
>>>     [junit]     at
>>> 
>> servletunit.struts.MockStrutsTestCase.verifyActionErrors(MockStrutsTestCase.
>>> java:765)
>>>     [junit]     at
>>> 
>> mori.web.controller.registry.test.TestBogusAction.testFormFailed(TestBogusAc
>>> tion.java:17)
>>>     [junit]     at
>>> sun.reflect.NativeMethodAccessorImpl.invoke0(Native
>>> Method)
>>>     [junit]     at
>>> 
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
>>> )
>>>     [junit]     at
>>> 
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
>>> .java:25)
>>>     [junit] TEST
>>> mori.web.controller.registry.test.TestBogusAction
>>> FAILED
>>> 
>>> 
>>> 
>>> Since the Form just creates an error, I should get
>>> exactly the opposite test
>>> results from what I'm getting, right? It seems as
>>> though the
>>> MockStrutsTestCase cannot see the errors generated
>>> by the DynaActionForm
>>> even though the source says it should do exactly
>>> that. Any help appreciated.
>>> 
>>> John
>>> 
>>> 
>>> 
>>> 
>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail:
>>> struts-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail:
>>> struts-user-help@jakarta.apache.org
>>> 
>>> 
>>> ------ End of Forwarded Message
>>> 
>>> 
>>> 
>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail:
>>> struts-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail:
>>> struts-user-help@jakarta.apache.org
>>> 
>> 
>> 
>> __________________________________
>> Do you Yahoo!?
>> Yahoo! Search - Find what you?re looking for faster
>> http://search.yahoo.com
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: [REPOST] MockStrutsTestCase problem

Posted by John Fitzpatrick <jo...@vitarara.net>.
Richard,

Thanks for the pointer to DynaValidatorForm. I can see how that would be
more useful than DynaActionForm for validation.

However, I tried making the change you suggest and I get the same result.
Any other advice? It's as if the ActionErrors generated by the form are
invisible to the test.

John


On 20040302 11:37 AM, "Richard Yee" <ry...@yahoo.com> wrote:

> John,
> In your form, you should extend
> org.apache.struts.validator.DynaValidatorForm instead
> of org.apache.struts.action.DynaActionForm
> 
> -Richard
> 
> 
> --- John Fitzpatrick <jo...@vitarara.net> wrote:
>> Sorry for the repost, but I really am stuck on this.
>> If anyone could help,
>> I'd appreciate it.
>> 
>> 
>> 
>> I'm having a problem setting up tests where errors
>> are created during
>> validation by a DynaActionForm using
>> MockStrutsTestCase. Here's my code:
>> 
>> The DynaActionForm:
>> ===================
>>     package test;
>> 
>>     import javax.servlet.http.*;
>> 
>>     import org.apache.struts.action.*;
>> 
>>     public class BogusForm extends
>> org.apache.struts.action.DynaActionForm {
>>         ActionErrors errors = new ActionErrors ();
>> 
>>        public ActionErrors validate ( ActionMapping
>> mapping,
>>                 HttpServletRequest request ) {
>>             
>>             errors.add ( "error", new ActionError (
>> "error.message" ) );
>>             return errors;
>>        }
>>     }
>> 
>> The Action:
>> ===========
>>     package test;
>> 
>>     import javax.servlet.http.*;
>> 
>>     import org.apache.struts.action.*;
>> 
>>     /**
>>      * @struts.action
>>      *    name="BogusForm"
>>      *    input=".doActivity"
>>      *    path="/Bogus"
>>      * @struts.action-forward
>>      *    name="doActivity"
>>      *    path=".doActivity"
>>      */
>>     public class BogusAction extends
>> org.apache.struts.action.Action {
>>        public ActionForward execute ( ActionMapping
>> mapping,
>>             ActionForm form, HttpServletRequest
>> request,
>>             HttpServletResponse response )
>>             throws Exception {
>> 
>>           return mapping.findForward ( "doActivity"
>> );
>>        }
>>     }
>> 
>> The MockStrutsTestCase:
>> =======================
>>     package test;
>> 
>>     import javax.servlet.*;
>>     import servletunit.struts.*;
>> 
>>     public class TestBogusAction extends
>> MockStrutsTestCase {
>>        public void testFormPassed () {
>>           setRequestPathInfo ( "/Bogus" );
>>           actionPerform ();
>>           verifyNoActionErrors ();
>>        }
>>        public void testFormFailed () {
>>           setRequestPathInfo ( "/Bogus" );
>>           actionPerform ();
>>           verifyActionErrors( new String [] {
>> "error.message" } );
>>        }
>>     }
>> 
>> The result:
>> ===========
>> test:
>>     [junit] Running
>> mori.web.controller.registry.test.TestBogusAction
>>     [junit] Tests run: 2, Failures: 1, Errors: 0,
>> Time elapsed: 2.435 sec
>>     [junit] Testsuite:
>> mori.web.controller.registry.test.TestBogusAction
>>     [junit] Tests run: 2, Failures: 1, Errors: 0,
>> Time elapsed: 2.435 sec
>> 
>>     [junit] Testcase: testFormPassed took 1.875 sec
>>     [junit] Testcase: testFormFailed took 0.547 sec
>>     [junit]     FAILED
>>     [junit] was expecting some error messages, but
>> received none.
>>     [junit] junit.framework.AssertionFailedError:
>> was expecting some
>> error messages, but received none.
>>     [junit]     at
>> 
> servletunit.struts.Common.verifyActionMessages(Common.java:84)
>>     [junit]     at
>> 
> servletunit.struts.MockStrutsTestCase.verifyActionErrors(MockStrutsTestCase.
>> java:765)
>>     [junit]     at
>> 
> mori.web.controller.registry.test.TestBogusAction.testFormFailed(TestBogusAc
>> tion.java:17)
>>     [junit]     at
>> sun.reflect.NativeMethodAccessorImpl.invoke0(Native
>> Method)
>>     [junit]     at
>> 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
>> )
>>     [junit]     at
>> 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
>> .java:25)
>>     [junit] TEST
>> mori.web.controller.registry.test.TestBogusAction
>> FAILED
>> 
>> 
>> 
>> Since the Form just creates an error, I should get
>> exactly the opposite test
>> results from what I'm getting, right? It seems as
>> though the
>> MockStrutsTestCase cannot see the errors generated
>> by the DynaActionForm
>> even though the source says it should do exactly
>> that. Any help appreciated.
>> 
>> John
>> 
>> 
>> 
>> 
> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> struts-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail:
>> struts-user-help@jakarta.apache.org
>> 
>> 
>> ------ End of Forwarded Message
>> 
>> 
>> 
> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> struts-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail:
>> struts-user-help@jakarta.apache.org
>> 
> 
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Search - Find what you?re looking for faster
> http://search.yahoo.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: [REPOST] MockStrutsTestCase problem

Posted by Richard Yee <ry...@yahoo.com>.
John,
In your form, you should extend
org.apache.struts.validator.DynaValidatorForm instead
of org.apache.struts.action.DynaActionForm

-Richard


--- John Fitzpatrick <jo...@vitarara.net> wrote:
> Sorry for the repost, but I really am stuck on this.
> If anyone could help,
> I'd appreciate it.
> 
> 
> 
> I'm having a problem setting up tests where errors
> are created during
> validation by a DynaActionForm using
> MockStrutsTestCase. Here's my code:
> 
> The DynaActionForm:
> ===================
>     package test;
> 
>     import javax.servlet.http.*;
> 
>     import org.apache.struts.action.*;
> 
>     public class BogusForm extends
> org.apache.struts.action.DynaActionForm {
>         ActionErrors errors = new ActionErrors ();
> 
>        public ActionErrors validate ( ActionMapping
> mapping,
>                 HttpServletRequest request ) {
>             
>             errors.add ( "error", new ActionError (
> "error.message" ) );
>             return errors;
>        }
>     }
> 
> The Action:
> ===========
>     package test;
> 
>     import javax.servlet.http.*;
> 
>     import org.apache.struts.action.*;
> 
>     /**
>      * @struts.action
>      *    name="BogusForm"
>      *    input=".doActivity"
>      *    path="/Bogus"
>      * @struts.action-forward
>      *    name="doActivity"
>      *    path=".doActivity"
>      */
>     public class BogusAction extends
> org.apache.struts.action.Action {
>        public ActionForward execute ( ActionMapping
> mapping,
>             ActionForm form, HttpServletRequest
> request,
>             HttpServletResponse response )
>             throws Exception {
> 
>           return mapping.findForward ( "doActivity"
> );
>        }
>     }
> 
> The MockStrutsTestCase:
> =======================
>     package test;
> 
>     import javax.servlet.*;
>     import servletunit.struts.*;
> 
>     public class TestBogusAction extends
> MockStrutsTestCase {
>        public void testFormPassed () {
>           setRequestPathInfo ( "/Bogus" );
>           actionPerform ();
>           verifyNoActionErrors ();
>        }
>        public void testFormFailed () {
>           setRequestPathInfo ( "/Bogus" );
>           actionPerform ();
>           verifyActionErrors( new String [] {
> "error.message" } );
>        }
>     }
> 
> The result:
> ===========
> test:
>     [junit] Running
> mori.web.controller.registry.test.TestBogusAction
>     [junit] Tests run: 2, Failures: 1, Errors: 0,
> Time elapsed: 2.435 sec
>     [junit] Testsuite:
> mori.web.controller.registry.test.TestBogusAction
>     [junit] Tests run: 2, Failures: 1, Errors: 0,
> Time elapsed: 2.435 sec
> 
>     [junit] Testcase: testFormPassed took 1.875 sec
>     [junit] Testcase: testFormFailed took 0.547 sec
>     [junit]     FAILED
>     [junit] was expecting some error messages, but
> received none.
>     [junit] junit.framework.AssertionFailedError:
> was expecting some
> error messages, but received none.
>     [junit]     at 
>
servletunit.struts.Common.verifyActionMessages(Common.java:84)
>     [junit]     at 
>
servletunit.struts.MockStrutsTestCase.verifyActionErrors(MockStrutsTestCase.
> java:765)
>     [junit]     at 
>
mori.web.controller.registry.test.TestBogusAction.testFormFailed(TestBogusAc
> tion.java:17)
>     [junit]     at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)
>     [junit]     at 
>
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
> )
>     [junit]     at 
>
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
> .java:25)
>     [junit] TEST
> mori.web.controller.registry.test.TestBogusAction
> FAILED
> 
> 
> 
> Since the Form just creates an error, I should get
> exactly the opposite test
> results from what I'm getting, right? It seems as
> though the
> MockStrutsTestCase cannot see the errors generated
> by the DynaActionForm
> even though the source says it should do exactly
> that. Any help appreciated.
> 
> John
> 
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 
> 
> ------ End of Forwarded Message
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you�re looking for faster
http://search.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org