jpa - 使用 Mockito 测试数据库删除方法
问题描述
我想在 PersonDao 类中测试我的删除方法。
public class PersonDao {
private EntityManager em;
Person person1 = new Person(111, "Klara", "Bamp", "qwerty123");
public PersonDao() {
}
public PersonDao(EntityManager em) {
this.em = em;
}
public void delete(Person p) {
em.remove(p);
}
}
那是我的测试课:
public class UnitTests {
@Mock
EntityManager entityManagerMock;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void deletePersonTestWithMockito() {
//ARRANGE
PersonDao personDao = new PersonDao(entityManagerMock);
when(entityManagerMock.find(Person.class, person1.getId())).thenReturn(person1);
//ACT
personDao.delete(person1);
//ASSERT
verify(entityManagerMock, times(1)).remove(person1);
assertNull(entityManagerMock.find(Person.class, person1.getId()));
}
我意识到,那 personDao.delete(person1) 什么都不做,因为我没有指定 entityManagerMock.remove 应该做什么。我试图将其指定为
when(entityManagerMock.remove(person1)).thenCallRealMethod();
,但收到错误Required type: T; Provided: void
解决方案
推荐阅读
- html - 为 HTML 突出显示语法指定嵌入窗口 Google 协作平台的框架大小
- javascript - 如何从 nodejs 向 wordpress 发出 post 请求
- android - 未定义对 'android::Parcel::writeString16(char16_t const*, unsigned int)' 的引用
- python - 使用 Tkinter 和 pandas 加载库 libstdc++.so.6 的冲突
- macos - 是否可以继承 NSButton,覆盖 mouseDown 和 mouseUp 事件并使用超类绘图方法
- javascript - 如何在reactjs中发出和断开套接字?
- c# - WPF 文档查看器不显示数据
- javascript - 如何确定每个 HTTP 请求的来源
- r - 使用 dplyr 到 group_by 并仅使用 if(没有 else)语句有条件地变异
- python - TypeError:参数 1 必须是 pygame.Surface,而不是我的 Python 游戏中的 pygame.Rect