• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bean Annotations and Junit testing  RSS feed

 
Ben Dunne
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bean that represents data been collected from a form on a jsp page. Currently I would like to validate my fields and write some test cases for them. As you can see from my test case example I test a string in the hope that it fails because it contains only one letter. My problem is my unit test is passing. The reason this is from what I can tell is that at runtime it fails when I try to persist my object using my entity manager. During my unit test I just I don’t call my entity manager I just try and set the field.

What I thought would happen was that when I use my bean fields set method the annotations would be checked and fail at that point. Hence why I expected my unit test in this case to fail.

What I would like to know is

1.Are annotations specifically designed to validate when I persist my object and am I using them incorrectly at this point?

2.Is this the best method to use to validate fields, is there a better way, should I write my own code to validate for me when I set my value?

a. Should I throw an exception from the set method of each bean field?

b. Are there any good online example of this done using jsp?

Unit Test:


Bean
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36453
459
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
The @Entity and related annotations are used by the container. When you run your unit test, you aren't in any container. You are in the regular Java flow so the annotations are ignored.

Your code is appropriate; the problem is that you need to write a JUnit integration test that actually tests in container. If you really do want the validation to occur without any container, you'd need to code them.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must manually validate the object using a javax.validation.Validator. You can get one using Validation.buildDefaultValidatorFactory().getValidator(). In my validation test cases I have a static Validator field that I initialize in an @BeforeClass method (and clean up in an @AfterClass method).

You can then call the validate method to validate the entire object, validateProperty to validate only a single property, or validateValue to validate only a value (without having to set it on an object). The returned set with ConstraintViolation can then be checked to see if the right number of violations is generated (0 if successful), and if they are the correct ones. That's why it's often easier to either use validateProperty, or make sure only one property is invalid. That way you get only one ConstraintViolation.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need a validation implementation on your classpath for that to work. I have the following included in my pom.xml:
With just this dependency the Hibernate validator is automatically added to my classpath when I run my tests, but during deployment the container's default implementation is used instead (which may be Hibernate as well but could be any other too).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!