ApplicationService { public List<ApplicationContent> loadApplications() { List<ApplicationContent> applications = new ArrayList<>(); List<Resource> applicationResources = applicationDiscovery.discoverApplications(); try { for (Resource applicationResource : applicationResources) { try (InputStream inputStream = applicationResource.getInputStream()) { applications.add(applicationReader.read(inputStream)); } } } catch (IOException e) { throw new AppliationLoadException("Unable to load application resource", e); } return applications; } ApplicationService(ApplicationDiscovery applicationDiscovery,
                              ApplicationReader applicationReader); List<ApplicationContent> loadApplications();  }
@Test public void shouldLoadApplications() throws Exception { Resource applicationResource = mock(Resource.class); given(applicationResource.getInputStream()).willReturn(mock(InputStream.class)); given(applicationDiscovery.discoverApplications()).willReturn(Collections.singletonList(applicationResource)); ApplicationContent applicationContent = new ApplicationContent(); given(applicationReader.read(applicationResource.getInputStream())).willReturn(applicationContent); List<ApplicationContent> applicationContents = applicationService.loadApplications(); assertThat(applicationContents).containsExactly(applicationContent); }
@Test public void shouldThrowApplicationLoadExceptionWhenIOExceptionOccurs() throws Exception { Resource applicationResource = mock(Resource.class); IOException ioException = new IOException(); given(applicationResource.getInputStream()).willThrow(ioException); given(applicationDiscovery.discoverApplications()).willReturn(Collections.singletonList(applicationResource)); Throwable thrown = catchThrowable(() -> applicationService.loadApplications()); assertThat(thrown).isInstanceOf(AppliationLoadException.class) .hasCause(ioException); }