How to test Valang validator?
Hi everybody!
Here's my problem.
Configured Valang validator for some Person class
Code:
<bean
id="personValidator"
class="&ValangValidator;">
<property name="valang">
<value>
<![CDATA[
{ name.first : ? is not blank : 'First Name is Empty' : '' }
{ name.last : ? is not blank : 'Last Name is Empty' : '' }
{ bornOn : ? is not blank : 'Born on field is empty' : '' }
]]>
</value>
</property>
</bean>
Wired it up with a controller
Code:
<bean
name="newPersonFormCotroller"
class="&PersonFormController;">
<property
name="validator"
ref="personValidator" />
</bean>
Works like a charm. Very kool. Yet how to test this runtime generated validator...
Code:
public class PersonValidatorTest extends TestCase {
Person person = new Person();
BindException errors = new BindException(person, "target");
Validator validator = Now, what???
}
Any ways, any clues?...
Instantiate via Spring config file?
I've never used Valang myself, but how about your test case:- Creates a FileSystemXmlApplicationContext that loads the beans from whichever XML file defines your validator.
- Calls getBean("personValidator") on that context to get the dynamically-generated Validator.
- Tests that Validator in the usual way.
Would this work?
How to test Valang validation for Server-side validation?
Hi!
I am trying to use Valang with annotations for server-side validation and wants to write JUnit tests for it. But, I haven't been able to find any documentation on this and the source jar doesn't contain any test classes (at least not to my knowledge).
My sample code looks this and it fails with a NPE since I don't have a applicationContext. How do I instead?
Code:
public class SimpleBeanTest
extends TestCase
{
public SimpleBeanTest(final String arg0)
{
super(arg0);
}
public void testValidation()
{
SimpleBean sb = new SimpleBean();
sb.setSimple("");
ValangValidator vv = new ValangValidator();
BindException errors = new BindException(sb, "target");
vv.validate(sb, errors);
assertEquals("Validation should fail with one (1) errors since length < 1 (0)", 1, errors.getErrorCount());
}
public class SimpleBean
{
@Length(min = 1, max = 255, message = "feature name must be between 1-255 characters")
private String simple;
public void setSimple(String simple)
{
this.simple = simple;
}
public String getSimple()
{
return this.simple;
}
}
}
Regards,
Jimisola