@Test public void testGet() { DBCollection coll = createMockCollection(); DBCursor cursor = createMockCursor(); expect(cursor.hasNext()).andAnswer(new IAnswer<Boolean>() { private int count = 0; @Override public Boolean answer() throws Throwable { count++; return count == 2 ? false : true; } }).anyTimes(); expect(cursor.next()).andAnswer(new IAnswer<DBObject>() { private int count = 0; @Override public DBObject answer() throws Throwable { count++; BasicDBObject dbObj = new BasicDBObject(); dbObj.put("title", "This is todo " + count); dbObj.put("completed", false); dbObj.put("order", count); dbObj.put("_id", new ObjectId("aaaaaaaaaaaaaaaaaaaaaaa" + count)); return dbObj; } }).anyTimes(); cursor.close(); expectLastCall(); replay(cursor); DBCursor emptyCursor = createMockCursor(); expect(emptyCursor.hasNext()).andReturn(false); replay(emptyCursor); expect(coll.find(eq(new BasicDBObject("_id", "aaaaaaaaaaaaaaaaaaaaaaa1")))).andReturn(cursor); expect(coll.find(eq(new BasicDBObject("_id", "1")))).andReturn(emptyCursor); replay(coll); MongoStore store = new MongoStore(coll); ToDo td = store.get("1"); assertNull(td); td = store.get("aaaaaaaaaaaaaaaaaaaaaaa1"); ToDo td1 = new ToDo(); td1.setId("aaaaaaaaaaaaaaaaaaaaaaa1"); td1.setTitle("This is todo 1"); td1.setOrder(1); assertEquals(td, td1); verify(cursor); verify(emptyCursor); verify(coll); }
@Override public ToDo get(String id) { BasicDBObject query = new BasicDBObject("_id", id); DBCursor cursor = coll.find(query); if(cursor.hasNext()) { ToDo td = createToDo(cursor.next()); cursor.close(); return td; } else { return null; } }
MongoStore implements ToDoStore { @Override public ToDo get(String id) { BasicDBObject query = new BasicDBObject("_id", id); DBCursor cursor = coll.find(query); if(cursor.hasNext()) { ToDo td = createToDo(cursor.next()); cursor.close(); return td; } else { return null; } } }
MongoStore implements ToDoStore { @Override public ToDo get(String id) { BasicDBObject query = new BasicDBObject("_id", id); DBCursor cursor = coll.find(query); if(cursor.hasNext()) { ToDo td = createToDo(cursor.next()); cursor.close(); return td; } else { return null; } } MongoStore(DBCollection coll); }
MongoStore implements ToDoStore { @Override public ToDo get(String id) { BasicDBObject query = new BasicDBObject("_id", id); DBCursor cursor = coll.find(query); if(cursor.hasNext()) { ToDo td = createToDo(cursor.next()); cursor.close(); return td; } else { return null; } } MongoStore(DBCollection coll); @Override Collection<ToDo> getAll(); @Override ToDo get(String id); @Override ToDo persist(ToDo td); @Override ToDo update(String id, ToDo td); @Override void delete(String id); @Override int count(); }
MongoStore implements ToDoStore { @Override public ToDo get(String id) { BasicDBObject query = new BasicDBObject("_id", id); DBCursor cursor = coll.find(query); if(cursor.hasNext()) { ToDo td = createToDo(cursor.next()); cursor.close(); return td; } else { return null; } } MongoStore(DBCollection coll); @Override Collection<ToDo> getAll(); @Override ToDo get(String id); @Override ToDo persist(ToDo td); @Override ToDo update(String id, ToDo td); @Override void delete(String id); @Override int count();  }