Files
course-registration/src/test/java/com/study/course_registration/ProdProfileContextTest.java
T

62 lines
2.1 KiB
Java

package com.study.course_registration;
import static org.assertj.core.api.Assertions.assertThat;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.postgresql.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;
import com.study.course_registration.support.Scenario;
@SpringBootTest
@ActiveProfiles("prod")
@Testcontainers
class ProdProfileContextTest {
private static final PostgreSQLContainer POSTGRES =
new PostgreSQLContainer(DockerImageName.parse("postgres:16-alpine"));
static {
POSTGRES.start();
}
@DynamicPropertySource
static void database(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", POSTGRES::getJdbcUrl);
registry.add("spring.datasource.username", POSTGRES::getUsername);
registry.add("spring.datasource.password", POSTGRES::getPassword);
registry.add("server.address", () -> "127.0.0.1");
}
@Autowired DataSource dataSource;
@Test
@DisplayName("prod profile → 빈 PostgreSQL에 Flyway 적용 후 context 기동")
void prodBootsWithFlyway() throws Exception {
try (Connection connection = dataSource.getConnection()) {
Scenario.given(connection)
.when(ProdProfileContextTest::isValid)
.then(valid -> assertThat(valid).isTrue());
}
}
private static boolean isValid(Connection connection) {
try {
return connection.isValid(1);
} catch (SQLException exception) {
throw new IllegalStateException("PostgreSQL connection validation failed", exception);
}
}
}