### Question:
SessionQueryManager { public Collection<SessionInfo> getAllSessions(Collection<String> serverIds) { List<SessionInfo> sessions = new LinkedList<SessionInfo>(); for (String server : serverIds) { SessionQueryType queryType = queryFactory.getSessionQueryType(server); Collection<SessionInfo> queriedSessions = queryType.getAllSessions(); if (debug.messageEnabled()) { debug.message(MessageFormat.format( "SessionQueryManager#getAllSessions() :: Queried {0} from: {1}", queriedSessions.size(), server)); } sessions.addAll(queriedSessions); } return sessions; } @Inject  SessionQueryManager(SessionQueryFactory queryFactory); Collection<SessionInfo> getAllSessions(Collection<String> serverIds);  }### Answer:
@Test public void shouldRetrieveQueryTypeForAllServerIds() { String badger = "Badger"; String weasel = "Weasel"; List<String> ids = new LinkedList<String>(); ids.add(badger); ids.add(weasel); SessionQueryFactory mockFactory = mock(SessionQueryFactory.class); given(mockFactory.getSessionQueryType(anyString())).willReturn(mock(SessionQueryType.class)); SessionQueryManager manager = new SessionQueryManager(mockFactory); manager.getAllSessions(ids); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(mockFactory, times(2)).getSessionQueryType(captor.capture()); List<String> capturedValues = captor.getAllValues(); assertTrue(capturedValues.contains(badger)); assertTrue(capturedValues.contains(weasel)); }
@Test public void shouldUseSessionQueryTypeProvidedByFactory() { SessionQueryType mockQueryType = mock(SessionQueryType.class); SessionQueryFactory mockFactory = mock(SessionQueryFactory.class); given(mockFactory.getSessionQueryType(anyString())).willReturn(mockQueryType); SessionQueryManager manager = new SessionQueryManager(mockFactory); manager.getAllSessions(Arrays.asList(new String[]{"badger"})); verify(mockQueryType, times(1)).getAllSessions(); }
@Test public void shouldReturnAllSessionsReturnedByQueryTypes() { String badger = "Badger"; String weasel = "Weasel"; SessionInfo one = mock(SessionInfo.class); SessionInfo two = mock(SessionInfo.class); SessionQueryType typeOne = mock(SessionQueryType.class); given(typeOne.getAllSessions()).willReturn(Arrays.asList(new SessionInfo[]{one})); SessionQueryType typeTwo = mock(SessionQueryType.class); given(typeTwo.getAllSessions()).willReturn(Arrays.asList(new SessionInfo[]{two})); SessionQueryFactory mockFactory = mock(SessionQueryFactory.class); given(mockFactory.getSessionQueryType(badger)).willReturn(typeOne); given(mockFactory.getSessionQueryType(weasel)).willReturn(typeTwo); SessionQueryManager manager = new SessionQueryManager(mockFactory); Collection<SessionInfo> sessions = manager.getAllSessions(Arrays.asList(new String[]{badger, weasel})); assertEquals(2, sessions.size()); assertTrue(sessions.contains(one)); assertTrue(sessions.contains(two)); }