Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

git statu# ElassandraUnit

ElassandraUnit is a fork form CassandraUnit modified for Elassandra.

ElassandraUnit helps you writing isolated JUnit tests in a Test Driven Development style with an embedded Elassandra instance.

Elassandra Unit Figure

  • Start an embedded Elassandra (including both Cassandra and Elasticsearch).
  • Create structure (keyspace and Column Families) and load data from an XML, JSON or YAML DataSet.
  • Execute a CQL script.
  • Query Cassandra through the Cassandra driver
  • Query Elasticsearch through the Elasticsearch REST API.

Quick start

Add the following dependencies:


Add the Elasticsearch REST high level client with the same version as the one embedded in elassandra.


To handle Elasticsearch search requests over CQL, set the system property cassandra.custom_query_handler_class to org.elassandra.index.ElasticQueryHandler:


Create a JUnit test class :

public class SimpleTest {

    static final String KEYSPACE = "ks";

    public static CassandraCQLUnit cassandraCQLUnit =
        new CassandraCQLUnit(new SimpleCQLDataSet(
            "CREATE TABLE users (email text PRIMARY KEY, firstname text, lastname text, es_query text, es_options text);", KEYSPACE));

    private static Mapper<User> userMapper;
    private static RestHighLevelClient client;

    public void setup() throws IOException {
        userMapper = new MappingManager(cassandraCQLUnit.session).mapper(User.class);
        client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));

        // create an elasticsearch index on table users
        CreateIndexRequest request = new CreateIndexRequest("users");
        request.mapping("users", XContentFactory.jsonBuilder()
                    .field("discover", ".*")
            .put("keyspace", KEYSPACE)                          // map index users to our keyspace.
            .put("index.synchronous_refresh",true)// synchronous elasticsearch refresh
        CreateIndexResponse createIndexResponse = client.indices().create(request);

    public void testMapper() throws Exception {
        User user1 = new User().withEmail("").withFirstname("Bob").withLastname("Smith");
        User user2 = new User().withEmail("").withFirstname("Alice").withLastname("Smith");
        User user3 = new User().withEmail("").withFirstname("Paul").withLastname("Dupont");;;;

        User user = userMapper.get("");
        assertThat(user, is(user1));

        // Elasticsearch search through the REST API
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.termQuery("lastname", "Smith"));
        SearchRequest searchRequest = new SearchRequest().indices("users").source(sourceBuilder);
        SearchResponse searchResponse =;
        assertThat(searchResponse.getHits().totalHits, is(2L));

        // Elasticsearch search through CQL
        String esQuery = new SearchSourceBuilder().query(QueryBuilders.termQuery("lastname", "Smith")).toString(ToXContent.EMPTY_PARAMS);
        ResultSet results = cassandraCQLUnit.session.execute(
            "SELECT * FROM users WHERE es_options = ? AND es_query = ? ALLOW FILTERING", "indices=users", esQuery);
        Result<User> users =;
        assertThat(users.all().size(), is(2));




This project is licensed under LGPL V3.0


  • CassandraUnit is developped by Jérémy Sevellec
  • Elasticsearch and Kibana are trademarks of Elasticsearch BV, registered in the U.S. and in other countries.
  • Apache Cassandra, Apache Lucene, Apache, Lucene and Cassandra are trademarks of the Apache Software Foundation.
  • Elassandra is a trademark of Strapdata SAS, registered in the U.S. and in other countries.


Utility tool to load Data into Elassandra to help you writing good isolated JUnit Test into your application




No packages published