Spring测试容器驱动程序org.testcontainers.jdbc.ContainerDatabaseDriver声明不接受jdbcUrl

dkqlctbz  于 2023-02-07  发布在  Spring
关注(0)|答案(3)|浏览(120)

我的集成测试具有以下配置,但遇到以下异常:
Driver org.testcontainers.jdbc.ContainerDatabaseDriver claims to not accept jdbcUrl, jdbc:postgresql://localhost:32864/test?loggerLevel=OFF

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WebApplication.class)
@AutoConfigureMockMvc
@Testcontainers
@TestPropertySource(ResourceUtils.CLASSPATH_URL_PREFIX + "application-test.properties")
public abstract class AbstractIntegrationTest {

    @Autowired
    protected MockMvc mockMvc;

    @Container
    protected static PostgreSQLContainer<?> postgresqlContainer = new PostgreSQLContainer<>();

    @DynamicPropertySource
    static void postgresqlProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", postgresqlContainer::getJdbcUrl);
        registry.add("spring.datasource.username", postgresqlContainer::getUsername);
        registry.add("spring.datasource.password", postgresqlContainer::getPassword);
    }

    @Test
    void contextLoads() {
        Assertions.assertThat(mockMvc).isNotNull();
        Assertions.assertThat(postgresqlContainer.isRunning()).isTrue();
    }
}

postgresqlContainer.getJdbcUrl()返回jdbc:postgresql://localhost:32864/test?loggerLevel=OFF,但它应该返回jdbc:tc:postgresql://...,它缺少tc部分。
有什么办法吗?
硬编码如下:String.format("jdbc:tc:postgresql://localhost:%s/%s", postgresqlContainer.getFirstMappedPort(), postgresqlContainer.getDatabaseName())似乎起作用了。
我到底做错了什么?

ugmeyewa

ugmeyewa1#

请看这里的大橙子警告:https://www.testcontainers.org/modules/databases/jdbc/
您应该使用带有tc:前缀和ContainerDatabaseDriver的JDBC URL,或者使用带有getJdbcUrl()的容器示例和原始驱动程序(或者让系统为您检测驱动程序),而不是两者都使用。

eoigrqb6

eoigrqb62#

在我的例子中,只是添加了postgresql依赖项(它包括驱动程序),它工作了:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.3.0</version>
</dependency>

我的测试类:

import org.junit.Rule;
import org.junit.Test;
import org.junit.platform.commons.annotation.Testable;
import org.testcontainers.containers.PostgreSQLContainer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.LogManager;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Testable
public class PostgreSqlContainerLiveTest {

    @Rule
    public PostgreSQLContainer postgresContainer = new PostgreSQLContainer("postgres:9.4");

    static {
        // Postgres JDBC driver uses JUL; disable it to avoid annoying, irrelevant, stderr logs during connection testing
        LogManager.getLogManager().getLogger("").setLevel(Level.OFF);
    }

    @Test
    public void whenSelectQueryExecuted_thenResultsReturned() throws Exception {
        ResultSet resultSet = performQuery(postgresContainer, "SELECT 1");
        resultSet.next();
        int result = resultSet.getInt(1);
        assertEquals(1, result);
    }

    private ResultSet performQuery(PostgreSQLContainer postgreSQLContainer, String query) throws SQLException {
        String jdbcUrl = postgreSQLContainer.getJdbcUrl();
        String username = postgreSQLContainer.getUsername();
        String password = postgreSQLContainer.getPassword();

        Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
        return conn.createStatement().executeQuery(query);
    }
}

我希望这能对你或其他人有所帮助。

rlcwz9us

rlcwz9us3#

确保在配置文件中同时包含dependency testcontainers postgresqldependency postgresql

相关问题