ExecutorService Отправить метод Return NULL - PullRequest
0 голосов
/ 02 октября 2019

У меня есть Служба Исполнителя, и я ее отправляю. Это возвращает будущее>. Мой код, как показано ниже,

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. Это правильный подход, или мне нужно что-то изменить в способе кодирования?

1 Ответ

0 голосов
/ 02 октября 2019

Похоже, что сопоставление аргументов в вашем макете не удалось.

Ваш макет использует новый экземпляр WordCountTask (который, в свою очередь, построен поверх пустого списка). Вы ожидаете, что макет сработает, имея другой новый экземпляр WordCountTask (построенный на основе результата работы функции splitFile , которая неизвестна в контексте)

Этоне понятно, почему сопоставление аргументов должно быть успешным.

...