ReportsService { public ExpensesByCategoryReport expensesByCategory() { ExpensesByCategoryReport report = new ExpensesByCategoryReport(); Set<Long> yearMonths = new HashSet<>(); Transactions transactions = transactionsService.retrieve(); for (Transaction transaction : transactions.getTransactions()) { Long yearMonth = yearMonth(transaction.getDate()); yearMonths.add(yearMonth); } List<Long> sortedMonths = new ArrayList<>(yearMonths); Collections.sort(sortedMonths); for (final Long yearMonth : sortedMonths) { report.add(detailsForMonth(yearMonth)); } return report; } @Inject  ReportsService(TransactionsService transactionsService); IncomeVersusExpensesReport incomeVersusExpenses(); ExpensesByCategoryReport expensesByCategory(); PeriodTransactions transactionsForMonth(long yearMonth); PeriodDetails detailsForMonth(long yearMonth);  }
@Test public void expensesByCategory() { transactions = createTransactionsWithCategories(); doReturn(transactions).when(transactionsService).retrieve(); ExpensesByCategoryReport expensesByCategory = service.expensesByCategory(); assertEquals("Monthly Expenses By Category", expensesByCategory.getTitle()); List<PeriodDetails> months = expensesByCategory.getMonthlyDetails(); assertNotNull(months); assertEquals(2, months.size()); assertEquals("January 2015", months.get(0).getName()); assertFalse(months.get(0).getCategories().isEmpty()); assertEquals("March 2015", months.get(1).getName()); assertFalse(months.get(1).getCategories().isEmpty()); }