Unable to write a spoc test case that covers an InterruptedException in catch block of CompletableFuture.allOf during code coverage

Viewed 31

I am unable to cover the InterruptedException block in the below code in code coverage while writing spoc test for a CompletableFuture.allOf method. Below is the code:

static void method1(
  ObjectA objA,
  CompletableFuture<Map<UUID, ObjectB>> futureMap1,
  CompletableFuture<Map<UUID, ObjectB>> futureMap2,
  CompletableFuture<Map<UUID, ObjectB>> futureMap3,
  CompletableFuture<Map<UUID, ObjectB>> futureMap4
) {
  try {
    CompletableFuture.allOf(
      futureMap1.thenAcceptAsync(
        response -> {
          if (objA.getAccount() != null && response.containsKey(objA.getAccount().getId())) {
            objA.setAccount(response.get(objA.getAccount().getId()));
          }
        }
      ),
      futureMap2.thenAcceptAsync(
        response -> {
          if (objA.getContact() != null && response.containsKey(objA.getContact().getId())) {
            objA.setContact(response.get(objA.getContact().getId()));
          }
        }
      ),
      futureMap3.thenAcceptAsync(
        response -> {
          if (objA.getEmployee() != null && response.containsKey(objA.getEmployee().getId())) {
            objA.setEmployee(response.get(objA.getEmployee().getId()));
          }
        }
      ),
      futureMap4.thenAcceptAsync(
        response -> {
          if (objA.getPartner() != null && response.containsKey(objA.getPartner.getId())) {
            objA.setPartner(response.get(objA.getPartner.getId()));
          }
        }
      )
    )
      .get();
  }
  catch (ExecutionException e) {
    log.error(EXECUTION_EXCEPTION_LOG, e);
  }
  catch (InterruptedException e) {
    log.warn(INTERRUPTED_EXCEPTION_LOG, e);
    Thread.currentThread().interrupt();
  }
}

Could someone please let me know how to write the test case for this block of code?

I tried with the below:

def "test"() {
  given:
  mockService1.getdetailsasMap(_) >> futureMap1
  mockService2.getdetailsasMap() >> futureMap2
  mockService3.getdetailsasMap(*) >> futureMap3
  mockService3.getdetailsasMap(_) >> futureMap4
  _ * helper.method1(_) >> { throw new InterruptedException() }

  when:
  mockobj.method1(obj)

  then:
  thrown(InterruptedException)
}

But it did not work.

0 Answers
Related