因此,它是我第一次使用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