You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by otisg <ot...@ureach.com> on 2003/02/20 18:06:40 UTC

Writing unit tests for Validator - Failing (setup help needed)

Hello,

I am trying to write unit tests for Commons
Validator.  I looked at the tests that come
with Validator and tried to copy the setup.
 However, the tests keep Failing (not the
same as not passing), and I suspect this is
due to something being misconfigured, but I
can't figure out what it is.

What I have is this:

1) A validator XML file (validator-rules.xml
and validation.xml merged into a single
file) in the same dir as the validator unit
test class:

<form-validation>
    <global>
        <validator name="StartLetter" 
classname="net.wgen.amp.validator.StartLetterValidator"
                  method="validateStartLetter"
            methodParams="java.lang.String,
                         
org.apache.commons.validator.ValidatorAction,
                         
org.apache.commons.validator.Field,
                         
org.apache.struts.action.ActionErrors,
                         
javax.servlet.http.HttpServletRequest"
                    
msg="amp.error.validator.invalidStartCharacter">
        </validator>
    </global>

    <formset>
        <form name="loginForm">
            <field property="username"
depends="required">
                <arg0 key="fields.username"/>
            </field>
            <field property="password"
depends="required">
                <arg0 key="fields.password"/>
            </field>
        </form>
    </formset>
</form-validation>


2) A unit test class that does this in
setUp() method (copied from Validator's tests):

  InputStream in = null;
  _resources = new ValidatorResources();

  in =
getClass().getResourceAsStream("validator-rules-amp.xml");
 
ValidatorResourcesInitializer.initialize(_resources,
in)


Then in my testXXX() methods I have (copied
from Validator's tests):

  ValueBean info = new ValueBean();
  info.setValue("ThisDOESStartWithALetter");
  valueTest(info, true);

This calls 'valueTest' helper method that is
again copied from Validator's tests and
contains this:

  Validator validator = new
Validator(_resources, FORM_KEY);
  validator.addResource(Validator.BEAN_KEY,
info);

  ValidatorResults results = null;
  results = validator.validate();
  assertNotNull("Results are null.", results);

  ValidatorResult result =
results.getValidatorResult("value");
  assertNotNull(ACTION + " value
ValidatorResult should not be null.", result);

....

It is this last assertNotNull that fails, so
I get this Failure message:

"StartLetter value ValidatorResult should
not be null."

That 'StartLetter' refers to the name of my
validation rule (see above).


I cannot figure out why this is happening.

My StartLetterValidator class looks like this:

public class StartLetterValidator
{
    public static boolean validateStartLetter(
        String str,
        ValidatorAction validatorAction,
        Field field,
        ActionErrors errors,
        HttpServletRequest request
        )
    {
        if (!Character.isLetter(str.charAt(0)))
        {
            errors.add(field.getKey(),
Resources.getActionError(request,
validatorAction, field));
        }

        return errors.isEmpty();
    }
}


Can anyone see what I'm doing wrong?
Everything looks correct to me.

The JUnit test should instruct Validator to
load the specified XML file, then Validator
should create an instance of
StartLetterValidator class, and my asserts
in the JUnit class should call its
validateStartLetter(...) method.

Can anyone tell where things are breaking?

Thanks.
Otis


________________________________________________
Get your own "800" number
Voicemail, fax, email, and a lot more
http://www.ureach.com/reg/tag