Harvinder Singh
Harvinder Singh

Reputation: 203

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{

}

}

Upvotes: 1

Views: 3561

Answers (2)

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24637

The solution by OP is no longer applicable to latest versions of Spring Boot, since CassandraUnitDependencyInjectionTestExecutionListener comes from cassandra-unit-spring that only works with JUnit 4, and the world has moved onto JUnit 5.

I've created a cassandra-unit-spring library that starts the Cassandra server and makes the ports available as Spring Boot environment properties.

Upvotes: 0

Harvinder Singh
Harvinder Singh

Reputation: 203

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.

Upvotes: 1

Related Questions