Search code examples
unit-testingcassandracassandra-2.0spring-data-cassandraembedded-cassandra

Spring-Cassandra Unit with Embedded Cassandra Dependency Injection Issue


I am having issue with Spring unit testing with Embedded Cassandra. The issue is both Embedded Cassandra and My Cassandra Server are starting at the same time. How to make sure that during unit testing only Embedded Cassandra Starts.

I am using spring-data for Cassandra.

I have the following Spring Config File.

cassandra.contactpoints=xxx.yyy.1.42
cassandra.keyspace=dialoguedev

My context file

    <?xml version='1.0'?>
    <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
   xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/cql http://www.springframework.org/schema/cql/spring-cql-1.0.xsd
  http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/spring-cassandra-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="com.xxx.cassandra" />
<context:property-placeholder location="classpath:cassandra.properties" />
<cassandra:cluster contact-points="${cassandra.contactpoints}"
        />
 <cassandra:session keyspace-name="${cassandra.keyspace}"
                   schema-action="RECREATE"
        />
<cassandra:mapping />
<cassandra:converter />

<cassandra:template />
<cassandra:repositories base-package="com.xxx.cassandra.repository" />

My test case is as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {"classpath:cassandrabeans.xml"})
@TestExecutionListeners( {  DependencyInjectionTestExecutionListener.class,    CassandraUnitDependencyInjectionTestExecutionListener.class  })
@EmbeddedCassandra

public class EventLogInsertTest  extends AbstractCassandraIntegrationTest {

@Autowired
private EventLogEventRepository eventLogEventRepository;

@Autowired
private EventLogPersonRepository eventLogPersonRepository;

@Test
public void runAllTableInsertTest()  throws Exception{

}

}


Solution

  • The issue is resolved by doing the following

    @TestExecutionListeners( {   CassandraUnitDependencyInjectionTestExecutionListener.class,DependencyInjectionTestExecutionListener.class })
    

    Please note that the order is important here. Now Embedded Cassandra will be started before repositories are auto wired.