I am encountering an issue with running integration tests in a Spring Boot application using JUnit 5 and Testcontainers. Despite following the configuration guidelines, my tests fail to start due to a bean creation exception related to the database connection.
Dependencies: I'm using Testcontainers version 1.19.3 with the following dependencies in my pom.xml:
<properties>
<testcontainers.version>1.19.3</testcontainers.version>
</properties>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
application.yml Configuration:
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:tc:postgresql:10.4://localhost:5432/test-tc
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
username: postgres
password: postgres
**Test Class: **
package com.worldline.ancv.messagebroker.web.rest;
import com.worldline.ancv.messagebroker.MessageBrokerApp;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = MessageBrokerApp.class)
public class TestOff {
@Test
public void getAllLogs() {
// Test implementation
}
}
**Erreur: **
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchDataSourceInitializer' defined in class path resource [org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration$DataSourceInitializerConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer]: Factory method 'batchDataSourceInitializer' threw exception; nested exception is java.lang.IllegalStateException: Failed to determine DatabaseDriver
...
Caused by: java.sql.SQLException: HikariDataSource HikariDataSource (HikariPool-9) has been closed.