Junit:将MyBatis升级到3.1.1后意外调用

时间:2012-06-01 11:39:09

标签: java junit mocking mybatis

情景:

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中与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(&lt; com.protogo.persistence.dao.CatalogRuleExample@a6ca0ea9>) 期望:   预期一次,从未被调用:catalogRuleDAO.selectByExample(&lt; com.protogo.persistence.dao.CatalogRuleExample@66e1beb1>);返回&lt; [com.protogo.persistence.data.CatalogRuleRecord@4f7d02c2,com.protogo.persistence.data.CatalogRuleRecord@4f7d02c2]&gt;

0 个答案:

没有答案