### Question:
CtsPersistenceOperationsMonitor { public Long getAverageDuration(TokenType tokenType) throws CoreTokenException { long result = 0l; if (tokenType == null) { throw new NullPointerException("TokenType cannot be null."); } final Collection<Long> queryResults = delegate.listDurationOfTokens(tokenType); if (queryResults.size() > 0) { for (Long entry : queryResults) { result += entry; } result /= queryResults.size(); } return result; } @Inject  CtsPersistenceOperationsMonitor(CtsPersistenceOperationsDelegate delegate); Long getTotalCount(); Long getTotalCount(TokenType tokenType); Long getAverageDuration(TokenType tokenType);  }### Answer:
@Test(expectedExceptions = NullPointerException.class) public void getAverageDurationTestErrorInvalidToken() throws CoreTokenException { final TokenType tokenType = null; opsMonitor.getAverageDuration(tokenType); }
@Test public void getAverageDurationTest() throws CoreTokenException { final Long duration = (long) 100; final TokenType tokenType = TokenType.SESSION; final ArrayList<Long> results = new ArrayList<Long>(); results.add(duration); given(delegate.listDurationOfTokens(tokenType)).willReturn(results); final Long result = opsMonitor.getAverageDuration(tokenType); assertEquals(duration, result); }
@Test public void getAverageDurationTestWithMultipleResults() throws CoreTokenException { final Long durationOne = (long) 100; final Long durationTwo = (long) 50; final Long durationAvg = (long) 75; final TokenType tokenType = TokenType.SESSION; final ArrayList<Long> results = new ArrayList<Long>(); results.add(durationOne); results.add(durationTwo); given(delegate.listDurationOfTokens(tokenType)).willReturn(results); final Long result = opsMonitor.getAverageDuration(tokenType); assertEquals(durationAvg, result); }
@Test public void getAverageDurationTestWithZeroResults() throws CoreTokenException { final Long durationAvg = (long) 0; final TokenType tokenType = TokenType.SESSION; final ArrayList<Long> results = new ArrayList<Long>(); given(delegate.listDurationOfTokens(tokenType)).willReturn(results); final Long result = opsMonitor.getAverageDuration(tokenType); assertEquals(durationAvg, result); }