Skip to content

Instantly share code, notes, and snippets.

@jaxkodex
Forked from tolbertam/MockExample.java
Last active October 5, 2017 15:17
Show Gist options
  • Save jaxkodex/b334b78a497e82db8bc328facb871c81 to your computer and use it in GitHub Desktop.
Save jaxkodex/b334b78a497e82db8bc328facb871c81 to your computer and use it in GitHub Desktop.
Session Mock Example
import com.datastax.driver.core.*;
import org.mockito.Mockito;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
/**
* Created by atolbert on 1/26/15.
*/
public class MockExample {
@Test
public void testMockExample() throws Exception {
Session session = Mockito.mock(Session.class);
// Mock a ResultSet that gets returned from ResultSetFuture#get()
ResultSet result = Mockito.mock(ResultSet.class);
List<Row> rows = new ArrayList<Row>();
Row r = Mockito.mock(Row.class);
Mockito.doReturn(5).when(r).getInt(0);
rows.add(r);
Mockito.doReturn(rows).when(result).all();
// Mock a ResultSetFuture that gets returned from Session#executeAsync()
ResultSetFuture future = Mockito.mock(ResultSetFuture.class);
Mockito.doReturn(result).when(future).get();
Mockito.doReturn(future).when(session).executeAsync(Mockito.anyString());
// Execute the query and print the 0th column of the first result.
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment