为了测试我的节目,我需要一个像嘲弄的方法调用:如何嘲笑嵌套方法的Java
entityManager.createQuery("SELECT...", Integer.class).getSingleResult()
的createQuery
部分返回TypedQuery<Integer>
,但我其实只是想返回一个整数: 1. 目前我正在使用Mockito为了创建我的嘲笑,我对此很新。
有没有一种测试方法?
谢谢!
为了测试我的节目,我需要一个像嘲弄的方法调用:如何嘲笑嵌套方法的Java
entityManager.createQuery("SELECT...", Integer.class).getSingleResult()
的createQuery
部分返回TypedQuery<Integer>
,但我其实只是想返回一个整数: 1. 目前我正在使用Mockito为了创建我的嘲笑,我对此很新。
有没有一种测试方法?
谢谢!
假设你有类EntityManager,Query。你可以像下面那样嘲笑你的测试。 (模拟(),任(),()时......方法在的Mockito)
int result = 1;
Query query = mock(Query.class);
EntityManager entityManager = mock(EntityManager.class);
when(entityManager.createQuery(any(), any()).thenReturn(query);
when(query.getSingleResult()).thenReturn(result);
谢谢。为我工作 –
嘲笑EntityManager
,然后你可以预先定义的返回值。 Mockito.doReturn(1).when(entityManagerMock).createQuery(any(String.class), any());
模拟结果,嘲笑结果。 –
是的。而已。谢谢!我认为那件事太复杂了。 –