java怎么测试dao_java-Mockito使用模拟对象测试DAO

我想测试这种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,但我不知道如何启动read方法来告诉她使用模拟方法道

解决方法:

您的代码不适合测试,尤其是这两行对测试非常不利:

EntityDao entityDao = new EntityDao();

UserDao userDao = new UserDao();

该代码应从此方法移至Factory或使用诸如Spring的容器注入(依赖注入).

仅Mockito不能测试这样的代码.您的方法只能做一件事,创建Daos是另一项工作.

标签:mocking,junit,dao,mockito,java

来源: https://codeday.me/bug/20191029/1963844.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Mockito模拟ThreadPoolExecutor对象可以通过以下步骤实现: 1. 导入Mockito库:在项目中引入Mockito库,可以通过Maven或Gradle等构建工具添加依赖。 2. 创建ThreadPoolExecutor对象模拟实例:使用Mockito的`mock()`方法创建一个ThreadPoolExecutor对象模拟实例。 ```java ThreadPoolExecutor executorMock = Mockito.mock(ThreadPoolExecutor.class); ``` 3. 设置模拟对象的行为:使用Mockito的`when().thenReturn()`方法设置模拟对象的方法调用行为。 ```java Mockito.when(executorMock.submit(Mockito.any(Runnable.class))).thenAnswer(invocation -> { Runnable task = invocation.getArgument(0); task.run(); return null; }); ``` 在上述示例中,我们使用`when().thenReturn()`方法来模拟ThreadPoolExecutor的`submit()`方法,当调用`submit()`方法时,我们获取传递给该方法的Runnable对象,并直接运行它。 4. 使用模拟对象进行测试:将模拟的ThreadPoolExecutor对象传递给需要测试的代码,并进行相应的测试。 ```java // 示例代码 ThreadPoolExecutor executor = executorMock; // 执行需要测试的代码 // ... ``` 至于在ThreadPoolExecutor重复调用dao的写法,可以按照以下步骤进行: 1. 创建一个实现了Runnable接口的任务类,该任务类负责执行需要重复调用的dao操作。 ```java public class DaoTask implements Runnable { private Dao dao; public DaoTask(Dao dao) { this.dao = dao; } @Override public void run() { // 执行dao操作 dao.doSomething(); } } ``` 2. 创建ThreadPoolExecutor对象,并提交多个DaoTask任务。 ```java ThreadPoolExecutor executor = new ThreadPoolExecutor(...); Dao dao = new Dao(); for (int i = 0; i < numberOfTasks; i++) { executor.submit(new DaoTask(dao)); } ``` 在上述示例中,我们创建了一个ThreadPoolExecutor对象,并使用循环提交了多个DaoTask任务,每个任务都会执行dao的操作。 3. 关闭ThreadPoolExecutor。 ```java executor.shutdown(); ``` 在所有任务执行完毕后,需要调用`shutdown()`方法来关闭ThreadPoolExecutor。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值