Skip to content

Commit d1cc8c3

Browse files
authored
test: flyway 스크립트를 검증하는 테스트 코드 작성 (#588)
* test: flyway 스크립트를 검증하는 테스트 코드 작성 * fix: DirtiesContext 어노테이션을 통해 기존 컨텍스트를 폐기하도록 - 새로운 MySQL 환경에서 마이그레이션이 이루어지도록 수정 * fix: flyway 검증용의 별도의 MySQL 컨테이너를 사용하도록 * chore: 테스트 의도를 쉽게 이해할 수 있도록 주석 추가 * chore: 명시적으로 컨테이너를 시작하도록 - 또한 MySQLTestContainer 코드와 유사한 컨벤션을 가지도록 수정
1 parent 513b59e commit d1cc8c3

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.example.solidconnection.database;
2+
3+
import com.example.solidconnection.support.RedisTestContainer;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.boot.test.util.TestPropertyValues;
7+
import org.springframework.context.ApplicationContextInitializer;
8+
import org.springframework.context.ConfigurableApplicationContext;
9+
import org.springframework.test.context.ContextConfiguration;
10+
import org.springframework.test.context.TestPropertySource;
11+
import org.testcontainers.containers.MySQLContainer;
12+
13+
@SpringBootTest
14+
@ContextConfiguration(initializers = {RedisTestContainer.class, FlywayMigrationTest.FlywayMySQLInitializer.class})
15+
@TestPropertySource(properties = {
16+
"spring.flyway.enabled=true",
17+
"spring.flyway.baseline-on-migrate=true",
18+
"spring.jpa.hibernate.ddl-auto=validate"
19+
})
20+
class FlywayMigrationTest {
21+
22+
private static final MySQLContainer<?> CONTAINER = new MySQLContainer<>("mysql:8.0")
23+
.withDatabaseName("flyway_test")
24+
.withUsername("flyway_user")
25+
.withPassword("flyway_password");
26+
27+
static {
28+
CONTAINER.start();
29+
}
30+
31+
static class FlywayMySQLInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
32+
@Override
33+
public void initialize(ConfigurableApplicationContext applicationContext) {
34+
TestPropertyValues.of(
35+
"spring.datasource.url=" + CONTAINER.getJdbcUrl(),
36+
"spring.datasource.username=" + CONTAINER.getUsername(),
37+
"spring.datasource.password=" + CONTAINER.getPassword()
38+
).applyTo(applicationContext.getEnvironment());
39+
}
40+
}
41+
42+
@Test
43+
void flyway_스크립트가_정상적으로_수행되는지_확인한다() {
44+
// Spring Boot 컨텍스트가 정상적으로 시작되면
45+
// Flyway 마이그레이션과 ddl-auto=validate 검증이 성공한 것
46+
}
47+
}

0 commit comments

Comments
 (0)