Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock completablefuture.get()?

This is the method I'm trying to mock:

@VisibleForTesting
public List<Row> processRows2(CompletableFuture future) {
    List<Row> 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 problem is that when I try to test it with this (currently just trying to get it to run all the way to either success or failure):

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

It hangs endlessly. My guess is that the future.get() is where it's hanging; I'm not sure. But I'm not sure how to mock that. I've tried this:

@Mock
private CompletableFuture mockFutures;

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

But this I feel is not correct. The try catch is because it yells at me about unhandled exceptions on the get(), so I don't know how to get around that.

I have also now tried this:

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

With the following in the setup:

    Mockito.doReturn(new ArrayList<Row>()).when(mockedFuture).get();

But it still hangs endlessly.

I've seen these:

How to mock completion of a CompletableFuture in Mockito This one I don't understand what exactly it's trying to get me to do, and doesn't feel super applicable, because it's not a get method. I saw some examples here that have .get() in them... but none were mocked methods unfortunately, they were gets in the test itself: https://www.javatips.net/api/java.util.concurrent.completablefuture

EDIT: the code runs. It returns results. So it isn't that the actual method isn't returning a value - I know it does this, it's doing it in QA right now.

like image 650
troyblerg Avatar asked Sep 04 '26 19:09

troyblerg


2 Answers

Your current CompletableFuture is not completed, so the .get() method hangs waiting for async completion that will never happen. You can use CompletableFuture.completedFuture(value) to create a CompletableFuture instance that will return the passed value when .get() is called on it.

like image 79
Pieter12345 Avatar answered Sep 06 '26 09:09

Pieter12345


You can use the CompletableFuture.completedFuture method here

@Test
public void processRows2test() {
    FeatureDaoImpl gar = new FeatureDaoImpl(connection);
    CompletableFuture L = CompletableFuture.completedFuture(new ArrayList<Row>());
    gar.processRows2(L);
}
like image 29
vaibhavsahu Avatar answered Sep 06 '26 07:09

vaibhavsahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!