Spring

Spring testcontainers Driver org.testcontainers.jdbc.ContainerDatabaseDriver는 jdbcUrl을 허용하지 않는다고 주장합니다.

기록만이살길 2022. 11. 7. 19:11
반응형

Spring testcontainers Driver org.testcontainers.jdbc.ContainerDatabaseDriver는 jdbcUrl을 허용하지 않는다고 주장합니다. 물어보다

1. 질문(문제점):

내 통합 테스트를 위해 다음과 같은 구성이 있는데 다음과 같은 예외가 발생했습니다.

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 해야 하며 tc 부분 jdbc:tc:postgresql://...이 누락되었습니다 .

이에 대한 해결책이 있습니까?

다음과 같이 하드코딩하면 효과가 String.format("jdbc:tc:postgresql://localhost:%s/%s", postgresqlContainer.getFirstMappedPort(), postgresqlContainer.getDatabaseName())있는 것 같습니다.

내가 여기서 뭘 잘못하고 있니?

2. 해결방안:

제 경우에는 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);
    }
}

이것이 귀하 또는 다른 사람에게 도움이 되기를 바랍니다.

61362092
반응형