Strategies for Simulating completableFuture.get()

One possible approach I thought of is to utilize a completion service to consume the futures, thereby releasing them, and then execute this on a separate thread to obtain new futures. It’s worth noting that I am not referring to the release of local callable variables upon thread completion.

Question:

The approach that I am attempting to imitate is as follows:

@VisibleForTesting
public List processRows2(CompletableFuture future) {
    List rows2 = new ArrayList<>();
    try {
        DefaultAsyncResultSet beep = (DefaultAsyncResultSet) future.get();
        for (Row b : beep.currentPage()) {
            rows2.add(b);
        }
    }
    catch (ExecutionException | InterruptedException e) {
        LOGGER.error(e);
        LOGGER.error(e.getStackTrace());
        throw new RuntimeException(e.getMessage() + " - Check thread pool resources are enough, may be too many queries in queue");
    }
    return rows2;
}

The issue lies in attempting to test it, with the current objective of running it until it reaches either success or failure.

@Test
public void processRows2test() {
    FeatureDaoImpl gar = new FeatureDaoImpl(connection);
    CompletableFuture L = new CompletableFuture();
    gar.processRows2(L);
}

The code seems to hang indefinitely, possibly due to the future.get() method. However, I’m unsure how to simulate this behavior for testing purposes. I have attempted to mock it, but with no success.

@Mock
private CompletableFuture mockFutures;
@Before
    public void setUp() {
        try {
            Mockito.when(mockFutures.get()).thenReturn((AsyncResultSet) mockResultSetFuture);
        }
        catch (Exception e) {
        }
    }

However, I believe that this is incorrect as I am getting unhandled exception errors on the get() and I am unsure of how to bypass them, hence the use of try-catch.

I have also now tried this:

@Mock
final CompletableFuture> mockedFuture = Mockito.mock(CompletableFuture.class);

With the following in the setup:

    Mockito.doReturn(new ArrayList()).when(mockedFuture).get();

But it still hangs endlessly.

I’ve seen these:

The process of simulating the completion of a CompletableFuture in Mockito is unclear to me. It doesn’t seem very relevant as it doesn’t involve a get method. While browsing through some examples, I came across some that contained .get() but none of them were methods that could be mocked, they were simply gets within the test code. You can check out examples at https://www.javatips.net/api/java.util.concurrent.completablefuture.

An update has been made on
code runs
. The method is functional and returns results. Even though I am aware of its capability to return data, it is currently being tested in QA.


Solution 1:

The

.get()

method is stuck due to the incomplete status of your current

CompletableFuture

. To resolve this issue, you can use the CompletableFuture.completedFuture(value) to generate a

CompletableFuture

object that will return the specified value upon calling

.get()

.


Solution 2:

You can use CompletableFuture.completeFuture method here
@Test
public void processRows2test() {
    FeatureDaoImpl gar = new FeatureDaoImpl(connection);
    CompletableFuture.completedFuture("donzo"); // in place of String you can place your desired result which might be returned when future is completed
    gar.processRows2(L);
}

Frequently Asked Questions