### Question:
LocalKinoRepository extends CrudRepository<LocalKinoDao, LocalKino> { public List<LocalKino> findAllByYear(boolean asc) { QueryBuilder.LOG_SQL = true; QueryBuilder.LOG_VALUES = true; QueryBuilder<LocalKino> localKinoQueryBuilder = dao.queryBuilder(); List<LocalKino> list = localKinoQueryBuilder.build().list(); Comparator<LocalKino> comparator; if (asc) { comparator = new Comparator<LocalKino>() { @Override public int compare(LocalKino o1, LocalKino o2) { int o1year = o1.getKino() != null ? o1.getKino().getYear() : 0; int o2year = o2.getKino() != null ? o2.getKino().getYear() : 0; if (o1year == o2year) { return 0; } return o1year < o2year ? -1 : 1; } }; } else { comparator = new Comparator<LocalKino>() { @Override public int compare(LocalKino o1, LocalKino o2) { int o1year = o1.getKino() != null ? o1.getKino().getYear() : 0; int o2year = o2.getKino() != null ? o2.getKino().getYear() : 0; if (o1year == o2year) { return 0; } return o1year < o2year ? 1 : -1; } }; } Collections.sort(list, comparator); return list; } LocalKinoRepository(DaoSession daoSession); LocalKino findByMovieId(long movieId); List<LocalKino> findAllByRating(boolean asc); List<LocalKino> findAllByYear(boolean asc); List<LocalKino> findAllByReviewDate(boolean asc); List<LocalKino> findAllByTitle(final boolean asc);  }### Answer:
@Test public void findAllByYearAsc() { TmdbKino oldTmdbKino = mock(TmdbKino.class); TmdbKino recentTmdbKino = mock(TmdbKino.class); doReturn(1987).when(oldTmdbKino).getYear(); doReturn(2012).when(recentTmdbKino).getYear(); LocalKino oldLocalKino = mock(LocalKino.class); doReturn(recentTmdbKino).when(localKino).getKino(); doReturn(oldTmdbKino).when(oldLocalKino).getKino(); QueryBuilder queryBuilder = mock(QueryBuilder.class); doReturn(queryBuilder).when(localKinoDao).queryBuilder(); Query query = mock(Query.class); doReturn(query).when(queryBuilder).build(); doReturn(Arrays.asList(localKino, oldLocalKino)).when(query).list(); assertEquals( Arrays.asList(oldLocalKino, localKino), new LocalKinoRepository(daoSession).findAllByYear(true) ); }
@Test public void findAllByYearDesc() { TmdbKino oldTmdbKino = mock(TmdbKino.class); TmdbKino recentTmdbKino = mock(TmdbKino.class); doReturn(1987).when(oldTmdbKino).getYear(); doReturn(2012).when(recentTmdbKino).getYear(); LocalKino oldLocalKino = mock(LocalKino.class); doReturn(recentTmdbKino).when(localKino).getKino(); doReturn(oldTmdbKino).when(oldLocalKino).getKino(); QueryBuilder queryBuilder = mock(QueryBuilder.class); doReturn(queryBuilder).when(localKinoDao).queryBuilder(); Query query = mock(Query.class); doReturn(query).when(queryBuilder).build(); doReturn(Arrays.asList(localKino, oldLocalKino)).when(query).list(); assertEquals( Arrays.asList(localKino, oldLocalKino), new LocalKinoRepository(daoSession).findAllByYear(false) ); }