1

I am using ExecutorCompletionService and below method calls from that

Future<List<Student>> studentDetails = taskCompletionService.take();
Lis<Student> details =studentDetails.get()

Now am writing Junit and Mockito and I want to mock above two calls. How can I achieve that?

halfer
  • 19,824
  • 17
  • 99
  • 186
Bravo
  • 8,589
  • 14
  • 48
  • 85

1 Answers1

1

If you simply want to stub for the take method of a mocked taskCompletionService then you could do something like this:

List<Student> studentDetails = Arrays.asList(fakeStudentDetail); 
when(taskCompletionService.take())
    .thenReturn(CompletableFuture.completedFuture(studentDetails));
David Rawson
  • 20,912
  • 7
  • 88
  • 124
  • This must be accepted as the answer. It helped me resolve my issue. Thanks David! – Raj Aug 28 '20 at 13:21