0

设想:

final CatalogRuleExample example = new CatalogRuleExample();
example.createCriteria().andCatalogIdEqualTo(catalogId);
example.setOrderByClause("position ASC");

final List<CatalogRuleRecord> records = new ArrayList<CatalogRuleRecord>();
final CatalogRuleRecord firstRecord = new CatalogRuleRecord();
final CatalogRuleRecord secondRecord = new CatalogRuleRecord();
records.add(firstRecord);
records.add(secondRecord);
mockery.checking(new Expectations() {
  {
    oneOf(catalogRuleDAO).selectByExample(example);
    will(returnValue(records));

...

问题:

CatalogRuleExample 的最后一个例子在我的 mockery.checking by selectByExample 中是不一样的。如果我覆盖 equals() 它将起作用。但我想知道,这种情况是否有更好的解决方案。在此先感谢 PS:相同的代码在我从 Ibatis 迁移到 Mybtis 之前有效

final CatalogRuleExample example = new CatalogRuleExample() {
  @Override
  public boolean equals(Object that) {
    if (that instanceof CatalogRuleExample) {
      CatalogRuleExample other = (CatalogRuleExample) that;
      if (other.getOredCriteria().size() != 1) {
        return false;
      }
      if (other.getOredCriteria().get(0).getCriteria().size() != 1) {
        return false;
      }
      if (!other.getOredCriteria().get(0).getCriteria().get(0).getValue().equals(catalogId)) {
        return false;
      }
      if (!other.getOrderByClause().equals("position ASC")) {
        return false;
      }
      return true;
    }
    return false;
  }
};

错误:意外调用:catalogRuleDAO.selectByExample(<com.protogo.persistence.dao.CatalogRuleExample@a6ca0ea9>) 期望:预期一次,从未调用过:catalogRuleDAO.selectByExample(<com.protogo.persistence.dao.CatalogRuleExample@66e1beb1>); 返回 <[com.protogo.persistence.data.CatalogRuleRecord@4f7d02c2, com.protogo.persistence.data.CatalogRuleRecord@4f7d02c2]>

4

0 回答 0