### Question:
WebsiteBatchLoader implements BatchLoader<Long, Website> { @Override public CompletionStage<List<Website>> load(List<Long> keys) { return CompletableFuture.supplyAsync(() -> { if (keys == null || keys.isEmpty()) { return Collections.emptyList(); } DynamicQuery query = DynamicQueryFactoryUtil.forClass(Website.class); query.add(PropertyFactoryUtil.forName("websiteId") .in(keys.stream() .mapToLong(l -> l) .toArray())); return websiteLocalService.dynamicQuery(query); }); }  @Reference(unbind = "-") void setWebsiteLocalService(WebsiteLocalService websiteLocalService); @Override CompletionStage<List<Website>> load(List<Long> keys); static final String KEY; }### Answer:
@Test public void load_should_return_an_object_list() throws ExecutionException, InterruptedException { List<Long> ids = Arrays.asList(1L, 2L, 3L); List<Object> expectedResults = new ArrayList<>(); IntStream.rangeClosed(1, 3) .forEach(value -> { Website entity = mock(Website.class); entity.setWebsiteId(value); expectedResults.add(entity); }); when(localService.dynamicQuery(any(DynamicQuery.class))) .thenReturn(expectedResults); CompletionStage<List<Website>> returnedResults = batchLoader.load(ids); assertNotNull(returnedResults); CompletableFuture<List<Website>> asyncResults; if (returnedResults instanceof CompletableFuture) { asyncResults = (CompletableFuture<List<Website>>) returnedResults; } else { asyncResults = returnedResults.toCompletableFuture(); } assertNotNull(asyncResults); List<Website> results = asyncResults.get(); assertNotNull(results); assertEquals(expectedResults, results); }
@Test public void load_with_empty_list_arg_should_return_an_empty_object_list() throws ExecutionException, InterruptedException { List<Long> ids = Collections.emptyList(); List<Object> expectedResults = Collections.emptyList(); when(localService.dynamicQuery(any(DynamicQuery.class))) .thenReturn(expectedResults); CompletionStage<List<Website>> returnedResults = batchLoader.load(ids); assertNotNull(returnedResults); CompletableFuture<List<Website>> asyncResults; if (returnedResults instanceof CompletableFuture) { asyncResults = (CompletableFuture<List<Website>>) returnedResults; } else { asyncResults = returnedResults.toCompletableFuture(); } assertNotNull(asyncResults); List<Website> results = asyncResults.get(); assertNotNull(results); assertEquals(expectedResults, results); }
@Test public void load_with_null_arg_should_return_an_empty_object_list() throws ExecutionException, InterruptedException { List<Object> expectedResults = Collections.emptyList(); when(localService.dynamicQuery(any(DynamicQuery.class))) .thenReturn(expectedResults); CompletionStage<List<Website>> returnedResults = batchLoader.load(null); assertNotNull(returnedResults); CompletableFuture<List<Website>> asyncResults; if (returnedResults instanceof CompletableFuture) { asyncResults = (CompletableFuture<List<Website>>) returnedResults; } else { asyncResults = returnedResults.toCompletableFuture(); } assertNotNull(asyncResults); List<Website> results = asyncResults.get(); assertNotNull(results); assertEquals(expectedResults, results); }