java - TooLittleActualInvocations:
问题描述
I try to use mock to verify method for serveral times.But I meet this problem.
org.mockito.exceptions.verification.TooLittleActualInvocations:
personDao.update(isA(com.zhaolu08.Person));
Wanted 3 times:
-> at com.zhaolu08.PersonServiceTest.testUpdate(PersonServiceTest.java:32)
But was 1 time:
while my code is:
package com.zhaolu08;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.isA;
import static org.mockito.Mockito.eq;
public class PersonServiceTest {
private PersonDao mockDao;
private PersonService personService;
@Before
public void setUp() throws Exception {
//模拟PersonDao对象
mockDao = mock(PersonDao.class);
when(mockDao.getPerson(1)).thenReturn(new Person(1, "Person1"));
when(mockDao.update(isA(Person.class))).thenReturn(true);
personService = new PersonService(mockDao);
}
@Test
public void testUpdate() throws Exception {
boolean result = personService.update(1, "new name");
Assert.assertTrue("must true", result);
verify(mockDao, times(2)).getPerson(eq(1));
verify(mockDao, times(3)).update(isA(Person.class));
}
}
I can't find out the problem. I try some methods. They did not work. My IDE is idea.
Maven pom is:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
I can't find out why it doesn't work. It is just a simple demo. It's too wired.
解决方案
It seems due to the fact that you are expecting personDao.update
to be invoked 3 times and actually in your method personService.update(1, "new name");
it is getting invoked only 1 time
推荐阅读
- flutter - 我们如何从 Windows 上在应用商店发布 Flutter 应用
- ios - 使用 ANY 和两个条件创建 NSPredicate
- elasticsearch - Scroll API 遗漏了一些文档
- javascript - 如何绕过 Angular API 调用中的响应部分
- apache-spark - spark - 长时间运行作业的性能调整
- spring-boot - ADFS 4.0 中的令牌自省端点
- agora.io - 在用户作为主持人或观众加入后,在 Agora GroupVoice 中调用鼠标单击事件不起作用
- javascript - 多个音频播放器的进度条
- jira - 如何在右侧 jira 数据中心问题视图页面中呈现页面
- reactjs - 如何将 Swiper 样式连接到 Next.js