当POJO引用另一个POJO时,Openpojo抛出BusinessException

时间:2013-07-29 14:11:54

标签: unit-testing jpa

我正在使用OpenPojo来自动化我的JPA实体上的测试。我对涉及其他实体的实体有麻烦。

示例:

public class Person {
    @BusinessKey
    private Integer id;

    ...getters/setters

    @Override
    public boolean equals(Object obj) {
            return BusinessIdentity.areEqual(this, obj);
    }

    @Override
    public int hashCode() {
            return BusinessIdentity.getHashCode(this);
    }
} 


public class Employee {
    @BusinessKey
    private Integer id;

    private Person person;

    ...getters/setters

    @Override
    public boolean equals(Object obj) {
            return BusinessIdentity.areEqual(this, obj);
    }

    @Override
    public int hashCode() {
            return BusinessIdentity.getHashCode(this);
    }
}

这是我的测试用例:

    // Create Rules to validate structure for POJO_PACKAGE
    pojoValidator.addRule(new NoPublicFieldsRule());
    pojoValidator.addRule(new NoPrimitivesRule());
    pojoValidator.addRule(new NoStaticExceptFinalRule());
    pojoValidator.addRule(new GetterMustExistRule());
    pojoValidator.addRule(new SetterMustExistRule());
    pojoValidator.addRule(new NoNestedClassRule());

    // Create Testers to validate behaviour for POJO_PACKAGE
    pojoValidator.addTester(new DefaultValuesNullTester());
    pojoValidator.addTester(new SetterTester());
    pojoValidator.addTester(new GetterTester());

    for (PojoClass pojoClass : pojoClasses) {
        pojoValidator.runValidation(pojoClass);
    }

我遇到以下异常:

com.openpojo.business.exception.BusinessException: Field required and can't be null [PojoFieldImpl

如果我从Employee类中删除了对Person的引用,则抛出的测试没有任何异常。

1 个答案:

答案 0 :(得分:2)

除非使用注释“@BusinessKey”,否则OpenPojo不会抛出此异常,而是列出@BusinessIdentity。你也没有展示你的equals和hashCode或toString实现,其中引用了“BusinessIdentity”?

另外需要注意的是,@ BusinessKey应该用于注释实际业务字段而不是您的DB代理ID(也称为主键)。