0

I am writing Junit test for a Class in which CompletableFuture is implemented. I am trying to mock this piece of code but the test gets stuck infinitely on CompletableFuture.allOf(future1,future2).join();

The class on which I am writing junit:

class Sample{
  @Autowired
  Executor executor;

  /* 
  piece of code
  */
  void SampleMethod(request){

    /*
    some piece of code
    */
    CompletableFuture<Obj1> future1 = CompletableFuture.SupplyAsync(
      (Supplier<Obj1>) () -> {
      return someClass.someMethod;
    },executor);

    CompletableFuture<Obj2> future2 = CompletableFuture.SupplyAsync(
      (Supplier<Obj1>) () -> {
      return someClass.someMethod;
    },executor);

    CompletableFuture.allOf(future1,future2).join();

}

My junit class looks like this:

class TestSample{
@Mock
Executor executor;

@Test 
void testSampleMethod(){

MockedStatic<CompletableFuture> mock1 = Mockito.mockedStatic(CompletableFuture.class);
mock.when(()->CompletableFuture.supplyAsync(any(),any())).thenReturn(completedFuture);
}
}

I am new to junit. How to write junit for this?

James Z
  • 12,209
  • 10
  • 24
  • 44
sk001
  • 29
  • 1
  • 9
  • An answer provided, if it suits you requirement please consider to upvote and accept it, otherwise feel free to leave comment, i will gladly try to help you. – Lunatic Nov 27 '22 at 11:50
  • Thanks for the suggestion @Lunatic . But i am getting future1 and future2 as null . Please suggest how to deal with this. – sk001 Nov 27 '22 at 12:29
  • Its a little bit unclear what is the main intention, you would like to mock two CompletableFuture, why mocking then? but the way that you create a new CompletableFuture and used '.allOf().join()' is going to complete once the appended CompletableFuture completes, and since they are not completed the main 'CompletableFuture.allOf(future1,future2).join()' wont be completed so. – Lunatic Nov 27 '22 at 14:31
  • What is your `completedFuture`? Anyway, I would advise against mocking `CompletableFuture` because [you don’t own it](https://stackoverflow.com/questions/1906344/should-you-only-mock-types-you-own). Instead, use the real thing and mock the types that return it. Also consider using Spring `@Async` to avoid working directly with `Executor`. – Didier L Nov 27 '22 at 15:18
  • i am getting both futures as null in the allOf() method. Also, I have mocked the Executor so i guess that's the reason for the test not working – sk001 Nov 30 '22 at 07:30

1 Answers1

0

You asked

The test stucks infinitely on CompletableFuture.allOf(future1,future2).join()

The mentioned future never get completed, i.e

CompletableFuture.allOf(future1,future2).join();

Actually CompletableFuture.allOf() returns a new CompletableFuture and is going to be completed when all of the given CompletableFutures completed and you mocked the futures in the which it seems the child futures wont complete, this result the experienced behavior.

Please also respect language model standards like naming convention of methods etc.

Lunatic
  • 1,519
  • 8
  • 24
  • I have tried removing the mocking but now I am getting null pointer exception as both of the future are coming as `null` – sk001 Nov 27 '22 at 12:19