因此,它是我第一次使用EasyMock,我正在尝试为一些遗留代码添加一些单元测试。Easymock:正在执行模拟
遗留代码是在Spring 3.1中,我使用的是EasyMock 3.4。
我在这里试图完成的是测试一个调用dao的服务(用Spring编写的一个方法)。
下面是代码:
@Entity
@Table(name="comment")
public class CommentBO{
public static CommentBO createNewComment(Integer clientNumber, Integer commentCategory){
CommentBO bo = new CommentBO();
bo.setClientNumber(clientNumber);
bo.setCommentCategory(commentCategory);
return bo;
}
}
public interface AssessmentService {
public CommentBO getComment(Integer clientNumber, Integer
commentCategory);
}
public class AssessmentServiceImpl implements
AssessmentService {
@Resource(name = "com.client.assessment.bl.dao.AssessmentDao")
private AssessmentDao assessmentDao;
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public CommentBO getComment(Integer clientNumber,Integer commentCategory) {
CommentBO comment = this.assessmentDao.getComment(
clientNumber, commentCategory);
if (comment != null && comment.getComments() != null) {
comment.setComments(comment.getComments().replaceAll("<li>•",
"<li>"));
}
return comment;
}
public interface AssessmentDao {
public CommentBO getComment(Integer clientNumber, Integer commentCategory);
}
@Repository(value = "com.client.assessment.bl.dao.AssessmentDao")
public class AssessmentDaoImpl implements AssessmentDao {
@Override
public CommentBO getComment(Integer clientNumber, Integer
commentCategory) {
Criteria criteria =
this.getSession(false).createCriteria(CommentBO.class);
criteria.add(Restrictions.eq("clientNumber", clientNumber))
.add(Restrictions.eq("commentCategory", commentCategory));
if (criteria.list() != null && criteria.list().size() > 0) {
return (CommentBO) criteria.list().get(0);
}
return null;
}
}
这里是我的单元测试写在了EasyMock
@SpringApplicationContext("classpath*:/com/client/assessment/**/*-context.xml")
public class AssessmentServiceTest extends UnitilsJUnit4 {
@SpringBean("com.client.assessment.remote.AssessmentService")
public AssessmentService assessmentService = null;
@Test
public void testGetComment(){
Integer clientNumber = 1;
Integer commentCategory = 1;
CommentBO commentBO = CommentBO.createNewComment(clientNumber, commentCategory);
AssessmentDao assessmentDao = EasyMock.createMock(AssessmentDao.class);
EasyMock.expect(assessmentDao.getComment((Integer) anyObject(), (Integer) anyObject())).andReturn(commentBO).anyTimes();
ReflectionTestUtils.setField(assessmentService, "assessmentDao", assessmentDao);
EasyMock.replay(assessmentDao);
CommentBO bo = assessmentService.getComment(clientNumber, commentCategory);
assertThat(bo , instanceOf(CommentBO.class));
}
}
所以基本上所发生的事情是,我的单元测试失败,因为
assessmentService.getComment(clientNumber, commentCategory);
结果
为空!
是的,这将是无效的,如果它实际上将被执行,因为在数据库中不存在与clientNumber没有记录= 1和commentCategory = 1
这就是为什么,我想嘲讽说,DAO,并迫使它的返回一个CommentBO对象。
正如我上面所说,它是我第一次使用EasyMock,所以我在这里错过了一些明显的东西?我是否需要模拟dao方法中的调用(即AssessmentDao的getComment)?但是如果我这样做,我会强制嘲笑标准对象等,我认为这是不好的做法?
好吧我编辑了代码。 AssessmentService.getComment不是一个静态调用。它应该是assessmentService.getComment。我尝试着放入EasyMock.verify(assessmentDao)并评论断言部分。测试通过成功,所以这意味着评估Dao正在被调用。然而,我仍然想测试这个断言部分--- assertThat(bo,instanceOf(CommentBO.class)) –
我想补充一下,当我创建一个有效的CommentBO时,意思是当我创建一个具有clientNumber的CommentBO和评论分类存在于数据库,断言工程(assertThat(bo,instanceOf(CommentBO.class))),但这是什么让我更混乱,因为我认为我嘲笑(或我是否存根?)assessmentDao.getComment返回CommentBO(无论传递给assessmentDao.getComment方法的clientnumber和commentcategory的值如何) –
您的属性名为'assessmentDao',您正在从'creditAssessmentDao'获得评论。我认为他们是一样的,但也许他们不是。另外,请使用'createMock',而不是'createNiceMock'来查看验证失败。 – Henri