【发布时间】:2014-01-30 17:36:11
【问题描述】:
我想测试这个 DAO 方法
//in GrabDao.java
public WrapperProject getChildren(Integer entityId, String entityType){
EntityDao entityDao = new EntityDao();
UserDao userDao = new UserDao();
EntityBase entity = entityDao.getEntityById(entityId, entityType);
Date dateProjet = userDao.getOrganismeFromSession().getExercice().getDateProjet();
return new Wrapper(dateProjet, entity);
}
这是我迄今为止尝试过的
//in GrabDaoTest.java
Integer paramEntityId = 1;
String paramEntityType = "type";
EntityBase entityBase = Mockito.mock(EntityBase.class);
EntityDao entityDao = Mockito.mock(EntityDao.class);
when(entityDao.getEntityById(paramEntityId, paramEntityType)).thenReturn(entityBase);
UserDao userDao = Mockito.mock(UserDao.class);
Organisme organisme = Mockito.mock(Organisme.class);
Exercice excercice = Mockito.mock(Exercice.class);
when(userDao.getOrganismeFromSession()).thenReturn(organisme);
when(organisme.getExercice()).thenReturn(excercice);
when(userDao.getOrganismeFromSession().getExercice().getDateProjet()).thenReturn(new GregorianCalendar(2000, 01, 01).getTime());
现在我想测试带有假参数 paramEntityId 和 paramEntityType 的 getChildren 将正确返回 WrapperProject 1 和 01/01/ 2000 using the mocked methods 但我不知道如何启动 read 方法告诉她使用 mocked dao
【问题讨论】:
标签: java junit mocking mockito dao