I'm trying to write a unit test using Mockito and Junit and creating mock objects, and the mock GroupManager has to return a list of users. It returns a Pager object, but Pager is an interface! I even tried to look at GroupManager souce code, but that is also an interface! How do I create a Pager object to return?
The JavaDocs doesn't even have the Pager class!
hi!
you found the groupmanager interface. why didn't you continue to look for an implementation class? you can find them close to the interface definitions.
in the impl.* subpackage you will find e.g.the HibernateGroupManager which implements the GroupManager interface.
getGroups() is implemented this way:
public Pager<Group> getGroups() throws EntityException { List<Group> result; try { result = getGroupsFromHibernate(); } catch (DataAccessException e) { throw new RepositoryException(e); } if (result == null) { return DefaultPager.emptyPager(); } return new DefaultPager<Group>(result); }
DefaultPager is in the sourcetree too:
public class DefaultPager<T> implements Pager<T> { private final List<T> page = new ArrayList<T>(); private Iterator<T> iter; private int index; public static <T> DefaultPager<T> emptyPager() { return new DefaultPager<T>(Collections.<T>emptyList()); } /** @deprecated since 2.0.3. Use the generic-friendly emptyPager() method */ public DefaultPager(){ this(Collections.<T>emptyList()); } public DefaultPager(Collection<T> col){ if (col != null) { page.addAll(col); } iter = page.iterator(); } public boolean isEmpty() { if (page == null) return true; return page.isEmpty(); } public Iterator<T> iterator() { return iter; } /** * @return a single, preloaded page. */ public List<T> getCurrentPage() { return new ArrayList<T>(page); } public void nextPage() { //do nothing, all contents are already within the currentPage } public boolean onLastPage() { return true; } public void skipTo(int index) throws PagerException { if (index < 0) throw new PagerException("Cannot skipTo a negative amount [" + index + "]"); int originalIndex = this.index; int distance; if (index > page.size()) { distance = page.size(); this.index = page.size(); } else { distance = index - this.index; this.index = index; } for (int i = originalIndex; i < distance; i++) iter.next(); } /** * @return the current index position of the pager */ public int getIndex() { return index; } /** * This pager always has all its items in a single page * @return zero, because this pager has all its items in a single page */ public int getIndexOfFirstItemInCurrentPage() { return 0; } public void remove() { throw new UnsupportedOperationException("This iterator does not support removal"); } public boolean hasNext() { return iter.hasNext(); } public T next() { T o = iter.next(); index++; return o; } }
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.