0

我正在使用 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 的引用,则测试不会引发任何异常。

4

1 回答 1

2

除非您使用注释“@BusinessKey”,否则 OpenPojo 不会抛出此异常,您正在列出 @BusinessIdentity。此外,您没有显示您的 equals 和 hashCode 或 toString 实现,“BusinessIdentity”在哪里被引用?

需要注意的另一件事是,@BusinessKey 应该用于注释实际业务字段,而不是您的数据库代理 ID(又名主键)。

于 2013-10-04T03:11:27.830 回答