@Test public void testFindAllByMakeAndYear() throws ParseException { Manufacturer ford = new Manufacturer("ford"); Manufacturer toyota = new Manufacturer("toyota"); Car focus = new Car(2008, ford, "Focus", "4 cylinder"); Car f150 = new Car(2010, ford, "F-150", "High HP"); Car camry = new Car(2011, toyota, "Camry", "Sedan"); Provider tiresPlus = new Provider("Tires Plus", "Bellevue"); WorkItem workItem1 = buildWorkItem("01/01/2012", focus, tiresPlus); WorkItem workItem2 = buildWorkItem("12/31/2012", f150, tiresPlus); WorkItem workItem3 = buildWorkItem("01/01/2013", camry, tiresPlus); WorkItem workItem4 = buildWorkItem("12/31/2013", f150, tiresPlus); WorkItem workItem5 = buildWorkItem("12/01/2013", focus, tiresPlus); List<WorkItem> allWorkItems = Arrays.asList(workItem1, workItem2, workItem3, workItem4, workItem5); when(workItemRepository.findAll()).thenReturn(allWorkItems); Collection<WorkItem> results = service.findAllByMakeAndYear("ford", "2013"); assertThat(results, containsInAnyOrder(workItem3, workItem5)); results = service.findAllByMakeAndYear("ford", "2012"); assertThat(results, containsInAnyOrder(workItem1, workItem2)); results = service.findAllByMakeAndYear("chevy", "2013"); assertThat(results, empty()); results = service.findAllByMakeAndYear("ford", "2014"); assertThat(results, empty()); }
@Test public void testFindAllByMakeAndYear_spy() throws ParseException { List<WorkItem> allWorkItems = Arrays.asList(new WorkItem(), new WorkItem()); when(workItemRepository.findAll()).thenReturn(allWorkItems); // Uses a spy on the service. Usually recommend refactoring service, but sometimes this is // necessary or just easier WorkItem expected = new WorkItem(); List<WorkItem> expectedList = Arrays.asList(expected); doReturn(expectedList).when(service).selectWorkItemsByMakeAndYear("bmw", "2008", allWorkItems); Collection<WorkItem> results = service.findAllByMakeAndYear("bmw", "2008"); // Showing nested matchers in contains and sameInstance assertThat(results, contains(sameInstance(expected))); }