VillageController { public String villages() { return cache.get(VILLAGE_LIST, new CacheableData<String>() { @Override public String fetch() { List<Village> villagesList = new ArrayList<Village>(); List<String> villages = allEligibleCouples.villages(); for (String village : villages) { villagesList.add(new Village(village)); } return new Gson().toJson(villagesList); } }); } VillageController(AllEligibleCouples allEligibleCouples, Cache<String> cache,
                             Cache<Villages> villagesCache); String villages(); Villages getVillages();  }
@Test public void shouldLoadVillages() throws Exception { List<Village> expectedVillages = asList(new Village("village1"), new Village("village2")); when(allEligibleCouples.villages()).thenReturn(asList("village1", "village2")); String villages = controller.villages(); List<Village> actualVillages = new Gson().fromJson(villages, new TypeToken<List<Village>>() { }.getType()); assertEquals(actualVillages, expectedVillages); }
@Test public void shouldLoadVillages() throws Exception { List<Village> expectedVillages = Arrays.asList(new Village("village1"), new Village("village2")); Mockito.when(allEligibleCouples.villages()).thenReturn(Arrays.asList("village1", "village2")); String villages = controller.villages(); List<Village> actualVillages = new Gson().fromJson(villages, new TypeToken<List<Village>>() { }.getType()); Assert.assertEquals(actualVillages, expectedVillages); }