嘲笑一个集合的方法
我有调用MongoDB集合以获取列表的方法。我想嘲笑列表的结果,但我在其中得到了NPE。
public String convertId(String Id, int counter) {
String id2 = "test";
List<Object> objectList = new ArrayList<>();
collection.find(eq(id,id2)).into(objectList);
Object obj = objectList.get(0);
return obj.getId();
}
我在Collection.find方法上获得了零指针。撰写测试案例是:
@Test
public void convertIdTest() {
List<Object> objectList = new ArrayList<>();
Document doc1 = new Document();
Mockito.when(collection.find(Mockito.any(BasicDBObject.class))).thenReturn(iterable);
Mockito.when(iterable.iterator()).thenReturn(cursor);
Mockito.when(cursor.hasNext()).thenReturn(true).thenReturn(false);
Mockito.when(cursor.next).thenReturn(doc1);
assertNotNull(request.converId("1233",1);
}
====================================
≈
FindIterable<Object> iterable = mock(FindIterable.class)
MongoCursor cursor = mock(MongoCursor.class)
I have method that is calling mongodb collection to get a list. I want to mock the outcome of the list but i am getting NPE on it.
public String convertId(String Id, int counter) {
String id2 = "test";
List<Object> objectList = new ArrayList<>();
collection.find(eq(id,id2)).into(objectList);
Object obj = objectList.get(0);
return obj.getId();
}
I am getting null pointer on collection.find method. The test case is wrote is :
@Test
public void convertIdTest() {
List<Object> objectList = new ArrayList<>();
Document doc1 = new Document();
Mockito.when(collection.find(Mockito.any(BasicDBObject.class))).thenReturn(iterable);
Mockito.when(iterable.iterator()).thenReturn(cursor);
Mockito.when(cursor.hasNext()).thenReturn(true).thenReturn(false);
Mockito.when(cursor.next).thenReturn(doc1);
assertNotNull(request.converId("1233",1);
}
≈========≈========================
iterable is of type:
FindIterable<Object> iterable = mock(FindIterable.class)
MongoCursor cursor = mock(MongoCursor.class)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论