简体   繁体   中英

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.

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM