Fixing Failed to load ApplicationContext with Cassandra in Spring

-

Fixing Failed to load ApplicationContext with Cassandra in Spring

The Problem

When running integration tests in a Spring Boot application that connects to Cassandra, you might hit this error:

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: com.datastax.oss.driver.api.core.connection.ControlConnection$NoHostAvailableException:
All hosts tried for query failed (tried: /127.0.0.1:9042 
(com.datastax.oss.driver.internal.core.connection.ControlConnection))

This means the Cassandra driver is attempting to connect to 127.0.0.1:9042, but it fails.


Why It Happens

Spring Boot loads the Cassandra configuration and assumes a database is available. The driver attempts to open a connection, but:

  • No Cassandra instance is running locally
  • Or the keyspace doesn’t exist
  • Or the tests aren’t supposed to use Cassandra at all

By default, integration tests load the same ApplicationContext as the application, including Cassandra beans.


Solutions

1. Start Cassandra Before Tests

If your tests require a real Cassandra instance:

  • Run Cassandra locally (cassandra -f) before executing tests.
  • Or use Docker:
docker run --name cassandra-test -p 9042:9042 -d cassandra:latest

Make sure the keyspace and schema are initialized before tests run.


2. Use Testcontainers for Cassandra

A modern approach is to spin up a Cassandra container automatically during tests:

@Testcontainers
@SpringBootTest
public class CassandraIntegrationTest {

    @Container
    static CassandraContainer<?> cassandra = 
        new CassandraContainer<>("cassandra:4.1")
            .withInitScript("schema.cql");

    @DynamicPropertySource
    static void cassandraProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.cassandra.contact-points", cassandra::getHost);
        registry.add("spring.cassandra.port", cassandra::getFirstMappedPort);
    }
}

This ensures tests always have a clean, disposable Cassandra instance.


3. Mock Repositories / Services

If your tests don’t actually need Cassandra:

  • Replace Cassandra repositories with mocks (e.g., Mockito).
  • Use @MockBean in your Spring Boot tests:
@MockBean
private MyCassandraRepository cassandraRepository;

This avoids hitting the database entirely.


4. Exclude Cassandra Auto-Configuration in Tests

If only some tests need Cassandra, you can exclude it for others:

@SpringBootTest
@EnableAutoConfiguration(exclude = { CassandraAutoConfiguration.class })
class NoCassandraTest {
    // test logic without Cassandra
}

Conclusion

The NoHostAvailableException isn’t a Spring bug—it just means your tests are trying to connect to Cassandra when none is available.

  • Start Cassandra manually or via Docker
  • Use Testcontainers for a portable test setup
  • Or mock/exclude Cassandra if it isn’t needed

With these fixes, you can upgrade your Spring Boot tests without being blocked by missing Cassandra hosts.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent comments