2

I have User entity and I try validate field password for length in my test.

My User Class:

public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "email", length = 100, nullable = false, unique = true)
    @NotEmpty
    @Email
    private String email;

    @Column(name = "password")
    @NotEmpty
    @Size(min = 5, max = 6)
    private String password;
}

There are 3 variants that I try.

1.When password's length < 5

@Test
public void passwordIsShort() {
    User user = new User();
    user.setPassword("1");

    Set<ConstraintViolation<User>> constraintViolations = validator.validateProperty(user, "password");

    assertEquals(1, constraintViolations.size());
}

This test doesn't work and shows error.

2.When password's length beetwen 5 and 6

@Test
public void passwordIsShort() {
    User user = new User();
    user.setPassword("12345");

    Set<ConstraintViolation<User>> constraintViolations = validator.validateProperty(user, "password");

    assertEquals(0, constraintViolations.size());
}

This test works correct.

3.When password's length > 6

@Test
public void passwordIsShort() {
    User user = new User();
    user.setPassword("1234567");

    Set<ConstraintViolation<User>> constraintViolations = validator.validateProperty(user, "password");

    assertEquals(1, constraintViolations.size());
}

This test doesn't works and shows error too

Error:

java.lang.NoSuchMethodError: javax.el.ExpressionFactory.newInstance()Ljavax/el/ExpressionFactory;
at org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTerm.<clinit>(InterpolationTerm.java:60)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateExpression(ResourceBundleMessageInterpolator.java:227)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateMessage(ResourceBundleMessageInterpolator.java:187)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolate(ResourceBundleMessageInterpolator.java:115)
at org.hibernate.validator.internal.engine.ValidationContext.interpolate(ValidationContext.java:370)
at org.hibernate.validator.internal.engine.ValidationContext.createConstraintViolation(ValidationContext.java:284)
at org.hibernate.validator.internal.engine.ValidationContext.createConstraintViolations(ValidationContext.java:246)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:289)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:133)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:91)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:85)
at org.hibernate.validator.internal.engine.ValidatorImpl.validatePropertyForDefaultGroup(ValidatorImpl.java:855)
at org.hibernate.validator.internal.engine.ValidatorImpl.validatePropertyForCurrentGroup(ValidatorImpl.java:768)
at org.hibernate.validator.internal.engine.ValidatorImpl.validatePropertyInContext(ValidatorImpl.java:670)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateProperty(ValidatorImpl.java:169)
at ru.yadoka.user.UserClass.passwordIsShort(UserClass.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

UPD

I use

java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) Client VM (build 24.51-b03, mixed mode, sharing)
Mufanu
  • 534
  • 7
  • 18
  • The constraint looks correct. What is the result of the test? No ConstraintViolation at all? Any exception? What if you use Validator.validate(user)? validateProperty should of course work as well, but it might help narrowing down the problem. – Hardy Jan 17 '14 at 16:02
  • I've edited my question and add some cases that I've tried. Also I've tried **Validator.validate(user)** and result was same. – Mufanu Jan 21 '14 at 04:50

2 Answers2

0

I am surprised that any of the tests would work. Your exception indicates that you have the wrong version of the Unified Expressions Language dependencies on your classpath. See also https://github.com/hibernate/hibernate-validator and http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-gettingstarted-uel

I guess the question becomes in which environment you are running and which EL dependencies (if any) you are using.

Hardy
  • 18,659
  • 3
  • 49
  • 65
  • 2
    I've added EL dependencies, no results. Then I try add message parameter `@Size(min = 5, max = 6, message = "size must be between 5 and 6") private String password;` and it works corect. It works correct without EL dependencies too. – Mufanu Jan 22 '14 at 06:23
0

I realize the post is bit old and probably resolved. Just wanted to updated anyway if someone else encounter the same issue. As Mufanu added in the comment this will resolve if we add message attribute in the annotation

@not null works fine without message

@Size(min = 5, max = 6, message = "size must be between 5 and 6")
private String password;

Rupesh Agrawal
  • 645
  • 8
  • 22