You should be able to validate with StrutsTestCase since it uses cactus.
You have access to the request through cactus, the ActionErrors is  stored
at Globals.ERROR_KEY or returned from your validate.  So, you can manually
instantiate ActionErrors.  You can manually instantiate a mapping, although
I doubt you use it in your validations.  You can manually populate your
form.  I test my validations through successful action results so I don't do
this but the following should work.

...Instantiate form...
MyForm form = new MyForm();
form.setMyProp("value");
... Other properties ...
ActionMapping map = new ActionMapping();
... You have access to the request in StrutsTestCase ...
HttpServlet req = this.getRequest();
... No sure about this but I don't think config is frozen if you declare the
map w/o config...
FowardConfig f = new ForwardConfig("name","url",false);
Map.addForwardConfig(f);
... No we can validate ...
ActionErrors ae = form.validate(map, req);
... You can use the strutstestcase valiations by sticking the errors in the
request
Req.setAttribute(Globals.ERROR_KEY, ae);
... I think you are done ...
verifyActionErrors(new String [] {"password.badpass"});

Edgar


> -----Original Message-----
> From: otisg [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, February 26, 2003 7:46 PM
> To: '[EMAIL PROTECTED]'
> Subject: Re: FW: Running unit tests for Validator used with Struts
> 
> 
> Hello Edgar,
> 
> I already am using StrutsTestCase to tests
> Actions I write.  I see how that could be
> used to test whether validation works, but
> unless I'm missing something testing of
> custom validators this way would really be
> just a side-effect of testing Actions.
> 
> So imagine you have an Action A that uses
> Validator V.  I write a STC-bases unit test
> and run it.  It passes, no problems.  One
> day somebody modifies A to stop using V and
> modifies V and breaks it.
> 
> My STC-bases tests keep running and passing,
> because A still works, but I will never find
> out that V is broken, because V is never
> really tested directly.
> 
> A new developer is hired and writes a new
> Action, AA, and decides to use V.  V is
> broken and his AA class is now broken
> because V is kaput.
> 
> One could argue that AA should have its unit
> tests, too, and that those should show me
> that V is (has been) broken....
> 
> So I will use STC for testing the Validator,
> too, but I'm wondering if there is better,
> more direct way of testing them?
> I can test them directly when they are
> Struts-independent, but when they depend on
> Struts (e.g. by taking some Struts classes
> as params), then I cannot.
> 
> Thanks,
> Otis
> 
> 
> 
> 
> ---- On Wed, 26 Feb 2003, Edgar P. Dollin
> ([EMAIL PROTECTED]) wrote:
> 
> >  
> > > Use StrutsTestCase,
> http://strutstestcase.sourceforge.net.  
> > > It does the whole struts cycle and
> allows test of all struts 
> > > components.
> > > 
> > > Edgar
> > > 
> > > > -----Original Message-----
> > > > From: otisg [mailto:[EMAIL PROTECTED]
> > > > Sent: Wednesday, February 26, 2003 4:51 PM
> > > > To: '[EMAIL PROTECTED]'
> > > > Subject: Q: Running unit tests for
> Validator used with Struts
> > > > 
> > > > 
> > > > Hello,
> > > > 
> > > > It is almost impossible to get anyone
> > > > attention on struts-user.
> > > > I'm using Struts and Validator with it.
> > > > I wrote a custom Validator and want to
> write
> > > > a unit test for it.
> > > > My Validator has a method like this:
> > > > 
> > > > public static boolean validateStartLetter(
> > > >     Object bean,
> > > >     ValidatorAction validatorAction,
> > > >     Field field,
> > > >     ActionErrors errors,
> > > >     HttpServletRequest request
> > > >     )
> > > > {
> > > >     String value =
> > > > ValidatorUtil.getValueAsString(bean,
> > > > field.getProperty());
> > > >     if
> > > >
> ((GenericValidator.isBlankOrNull(value)) ||
> > > > !Character.isLetter(value.charAt(0)))
> > > >     {
> > > >         errors.add(field.getKey(), 
> > > > Resources.getActionError(request,
> validatorAction, field));
> > > >     }
> > > > 
> > > >     return errors.isEmpty();
> > > > }
> > > > 
> > > > I am running JUnit from Ant to run my unit
> > > > test class.
> > > > Apparently this doesn't work because the
> > > > instance of ActionErrors passed to this
> > > > method is null, causing a NPE.
> > > > 
> > > > I wanted to check the unit tests for
> > > > Validator in Struts (FieldChecks) to
> see how
> > > > you wrote your unit tests, so that
> this NPE
> > > > is avoided, but I could not find any unit
> > > > tests for it.
> > > > Are there really no unit tests there?
> > > > 
> > > > Regardless, how would you go about running
> > > > unit tests for a custom validator from
> > > > something like Ant?
> > > > 
> > > > It seems to me that doing that may be
> > > > impossible, because there is no Struts
> > > > running to instantiate ActionErrors, there
> > > > is no HttpServletRequest, and so on.
> > > > 
> > > > Is there a way to do this?
> > > > 
> > > > Thanks,
> > > > Otis
> > > > 
> > > > 
> > > >
> ________________________________________________
> > > > Get your own "800" number
> > > > Voicemail, fax, email, and a lot more
> http://www.ureach.com/reg/tag
> > > > 
> > > > 
> > >
> ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> > > > For additional commands, e-mail:
> [EMAIL PROTECTED]
> > > > 
> > > 
> > 
> > 
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> > For additional commands, e-mail:
> [EMAIL PROTECTED]
> > 
> > 
> > 
> 
> 
> ________________________________________________
> Get your own "800" number
> Voicemail, fax, email, and a lot more http://www.ureach.com/reg/tag
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to