У меня есть Служба Исполнителя, и я ее отправляю. Это возвращает будущее>. Мой код, как показано ниже,
for (int i = 0; i < firstSplitFiles.size(); i++) {
Future<Map<String, AtomicInteger>> result = executorService.submit(new WordCountTask(firstSplitFiles, firstFile.getName()));
firstFileCounterMap = result.get();
}
Я написал модульное тестирование, как показано ниже,
@Mock
private Future<Map<String, AtomicInteger>> firstFileWordCounter;
@Mock
private Future<Map<String, AtomicInteger>> secondFileWordCounter;
when(executorService.submit(new WordCountTask(Collections.emptyList(), firstFile.getName()))).thenReturn(firstFileWordCounter);
when(executorService.submit(new WordCountTask(Collections.emptyList(), secondFile.getName()))).thenReturn(secondFileWordCounter);
when(firstFileWordCounter.get()).thenReturn(firstCounterMap);
when(secondFileWordCounter.get()).thenReturn(secondCounterMap);
FileReadHelper.splitAndReadProcess(firstFile, secondFile, executorService, 21);
и мой метод splitAndReadProcess, как показано ниже,
public static void splitAndReadProcess(File firstFile, File secondFile, ExecutorService executorService, int splitSize) throws
IOException, InterruptedException, ExecutionException {
AtomicInteger atomicInteger = new AtomicInteger(INITIAL_COUNTER_VALUE);
Map<String, AtomicInteger> firstFileCounterMap = null;
Map<String, AtomicInteger> secondFileCounterMap = null;
List<File> firstSplitFiles = splitFile(firstFile, splitSize, atomicInteger);
List<File> secondSplitFiles = splitFile(secondFile, splitSize, atomicInteger);
logger.info("Reading File(s) process started!");
for (int i = 0; i < firstSplitFiles.size(); i++) {
Future<Map<String, AtomicInteger>> result = executorService.submit(new WordCountTask(firstSplitFiles, firstFile.getName()));
firstFileCounterMap = result.get(); // NULL RESULT
}
for (int j = 0; j < secondSplitFiles.size(); j++) {
Future<Map<String, AtomicInteger>> result = executorService.submit(new WordCountTask(secondSplitFiles, secondFile.getName()));
secondFileCounterMap = result.get();
}
}
I'mполучить результат как NULL. Это правильный подход, или мне нужно что-то изменить в способе кодирования?