3

我对 JUnit 测试一种方法有疑问。我使用 EasyMock.createMock 来模拟 someDao。我仍然得到easymock unexpected method call expected 1 actual 0 someDato.findByCriteria(criteria, -1, -1) 这就是代码的样子(它只是草稿)。我怀疑问题出在criteria对象上。错误点线return someDato.findByCriteria(criteria, -1, -1);

@Test
public void findDataSericeTest(){
    DetachedCriteria criteria = DetachedCriteria.forClass(SomeClass.class);
    criteria.add(Restictions.eq("sth1",string1));
    criteria.add(Restictions.eq("sth2",string2));

    expect(someDato.findByCriteria(criteria, -1, -1)).andReturn(SomeClass);
    replay(someDao);
    serviceTested.findDataService("string1","string2");

    EasyMock.verify(someDao);

}

 public SomeClass findDataService(String string1, String string2){
    DetachedCriteria criteria = DetachedCriteria.forClass(SomeClass.class);
    criteria.add(Restictions.eq("sth1",string1));
    criteria.add(Restictions.eq("sth2",string2));

    return someDato.findByCriteria(criteria, -1, -1);
}

有任何想法吗?

4

1 回答 1

2

我敢打赌DetachedCriteria没有实现 / 覆盖equalshashcode. 验证将使用equals预期参数的方法来查看它是否与传递的实际参数匹配。

于 2013-05-28T16:53:37.647 回答