Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit test of future resultset

I have been mocking a test for a function ResultSetFuture, I used a example from Github (that works) for testing ResultSetFuture, but the code I am testing/mocking uses Futures#successfulAsList as shown here. So in Line 34, the test just halts and never finishes. The code shown below is a portion of the test that halts.

ResultSetFuture future = Mockito.mock(ResultSetFuture.class);
Mockito.doReturn(result).when(future).get();
Mockito.doReturn(future).when(session).executeAsync(Mockito.anyString());

ResultSetFuture resultF = session.executeAsync("select value from table where key='a'");

Future<List<ResultSet>> data = Futures.successfulAsList(new ArrayList(){{ add(resultF); }});
List finished = data.get(); //  <---- The test stops here
like image 416
jaxkodex Avatar asked Oct 29 '22 01:10

jaxkodex


1 Answers

You need to mock the isDone method in future to indicate that the execution finished and avoid the code halting.

ResultSetFuture future = Mockito.mock(ResultSetFuture.class);
Mockito.doReturn(result).when(future).get();
Mockito.doReturn(future).when(session).executeAsync(Mockito.anyString());
Mockito.doReturn(true).when(future).isDone(); //<-- mock to avoid halting

ResultSetFuture resultF = session.executeAsync("select value from table where key='a'");

Future<List<ResultSet>> data = Futures.successfulAsList(Collections.singletonList(resultF));
List finished = data.get(); //  <---- The test stops here
like image 170
alayor Avatar answered Nov 15 '22 04:11

alayor