Testing
@SpringBootTest@TestInstance(TestInstance.Lifecycle.PER_CLASS)class PersonRepositoryTest @Autowired constructor( private val repository: PersonRepository) { @Test fun `should find person by city`() { val results = repository.findByCity("New York") assertThat(results).isNotEmpty }}Automated Test Configuration (Testcontainers + Local Dev)
Section titled “Automated Test Configuration (Testcontainers + Local Dev)”Drivine provides @EnableDrivineTestConfig for seamless test setup that works in both local development and CI:
1. Define datasource in application-test.yml:
database: datasources: neo: host: localhost port: 7687 user-name: neo4j password: password type: NEO4J database-name: neo4j2. Use @EnableDrivineTestConfig in your test configuration:
@Configuration@EnableDrivine@EnableDrivineTestConfigclass TestConfig3. Control behavior with environment variable:
# Use local Neo4j (for development - fast, inspectable)export USE_LOCAL_NEO4J=true./gradlew test
# Use Testcontainers (for CI - isolated, default)./gradlew test # USE_LOCAL_NEO4J defaults to falseWhat happens automatically:
- Local Mode (
USE_LOCAL_NEO4J=true): Uses your application-test.yml settings as-is, connects to your local Neo4j - CI Mode (default): Starts a Neo4j Testcontainer automatically and overrides host/port/password from your properties
The Testcontainer runs neo4j:latest (DrivineTestContainer.DEFAULT_IMAGE), so a new Neo4j release reaches your tests as soon as it is out. Choose another with NEO4J_TEST_IMAGE or -Dtest.neo4j.image, but not Neo4j 5.26.0 to 5.26.3, 2025.01 or 2025.02, which write the wrong relationship type for a dynamic relationship type (neo4j/neo4j#13597), fixed in 2025.03 and 5.26.4. See TEST_CONTAINERS.md.
Benefits:
- ✅ One configuration works for both local dev and CI
- ✅ Zero boilerplate - no manual container setup
- ✅ Fast local development with real Neo4j
- ✅ Reliable CI with Testcontainers
- ✅ Easy debugging - set
@Rollback(false)and inspect your local DB
Manual TestContainers Setup
Section titled “Manual TestContainers Setup”If you need more control, you can still configure Testcontainers manually:
@Configuration@EnableDrivineclass TestConfig { @Bean fun dataSourceMap(): DataSourceMap { val props = ConnectionProperties( host = extractHost(DrivineTestContainer.getConnectionUrl()), port = extractPort(DrivineTestContainer.getConnectionUrl()), userName = DrivineTestContainer.getConnectionUsername(), password = DrivineTestContainer.getConnectionPassword(), type = DatabaseType.NEO4J, databaseName = "neo4j" ) return DataSourceMap(mapOf("neo" to props)) }
private fun extractHost(boltUrl: String): String = boltUrl.substringAfter("bolt://").substringBefore(":")
private fun extractPort(boltUrl: String): Int = boltUrl.substringAfter("bolt://").substringAfter(":").toIntOrNull() ?: 7687}
