JSR 303,在POJO中使用默认构造函数时不抛出ValidationException

时间:2011-11-11 15:33:04

标签: java validation bean-validation

第一次和第二次测试成功(这意味着抛出了验证),但第三次测试失败。 由于我正在使用的框架的要求,我必须有一个我的POJO的默认构造函数。如第二个测试中所示,验证在使用默认构造函数时确实有效,但由于某种原因,不会抛出ValidationException,如第三个测试中所示。 有谁知道为什么第三次测试失败了?

我将Spring Data Graph与Neo4J数据库一起使用。

@Test(expected = ValidationException.class) //succeds
public void shouldThrowValidationException() {
    Employee employee = new Employee(null, "Stevens");
    employee.persist();
}

@Test() //succeds
public void shouldNotThrowValidationException() {
    Employee employee = new Employee();
    employee.persist();
    assertEquals(1, validator.validate(employee).size()); 
    //validate returns a collection of ConstraintViolations
}

@Test(expected = ValidationException.class) //fails
public void shouldThrowValidationException2() {
    Employee employee = new Employee();
    employee.persist();
}


@NodeEntity
public class Employee {

    @NotNull
    @Size(min = 2, max = 20)
    private String firstname;
    private String lastname;

    public Employee() {
    }

    public Employee(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }
}

0 个答案:

没有答案