Spring testcontainers Driver org.testcontainers.jdbc.ContainerDatabaseDriver 声称不接受 jdbcUrl
Spring testcontainers Driver org.testcontainers.jdbc.ContainerDatabaseDriver claims to not accept jdbcUrl
我的集成测试配置如下,我 运行 出现以下异常:
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()
return jdbc:postgresql://localhost:32864/test?loggerLevel=OFF
但它应该 return jdbc:tc:postgresql://...
,它缺少 tc 部分。
有什么解决办法吗?
硬编码:String.format("jdbc:tc:postgresql://localhost:%s/%s", postgresqlContainer.getFirstMappedPort(), postgresqlContainer.getDatabaseName())
似乎可行。
我做错了什么?
请看这里的大橙色警告:
https://www.testcontainers.org/modules/databases/jdbc/
您应该使用带有 tc:
前缀和 ContainerDatabaseDriver
的 JDBC URL 或带有 getJdbcUrl()
和原始驱动程序的容器实例(或者让系统会为您检测驱动程序),而不是两者。
在我的例子中,只是添加了 postgresql 依赖项(它包括驱动程序)并且它起作用了:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.0</version>
</dependency>
我的测试class:
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);
}
}
希望对您或其他人有所帮助。
我的集成测试配置如下,我 运行 出现以下异常:
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()
return jdbc:postgresql://localhost:32864/test?loggerLevel=OFF
但它应该 return jdbc:tc:postgresql://...
,它缺少 tc 部分。
有什么解决办法吗?
硬编码:String.format("jdbc:tc:postgresql://localhost:%s/%s", postgresqlContainer.getFirstMappedPort(), postgresqlContainer.getDatabaseName())
似乎可行。
我做错了什么?
请看这里的大橙色警告: https://www.testcontainers.org/modules/databases/jdbc/
您应该使用带有 tc:
前缀和 ContainerDatabaseDriver
的 JDBC URL 或带有 getJdbcUrl()
和原始驱动程序的容器实例(或者让系统会为您检测驱动程序),而不是两者。
在我的例子中,只是添加了 postgresql 依赖项(它包括驱动程序)并且它起作用了:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.0</version>
</dependency>
我的测试class:
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);
}
}
希望对您或其他人有所帮助。