diff --git a/build.gradle b/build.gradle index 5375948..d77a5b1 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id 'java' - id 'org.springframework.boot' version '3.5.6' + id 'org.springframework.boot' version '3.5.9' id 'io.spring.dependency-management' version '1.1.7' } @@ -35,8 +35,8 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' - // Testcontainers for integration tests - testImplementation 'org.testcontainers:testcontainers:1.19.3' + // Test Containers + testImplementation 'org.springframework.boot:spring-boot-testcontainers' testImplementation 'org.testcontainers:mysql:1.19.3' testImplementation 'org.testcontainers:junit-jupiter:1.19.3' diff --git a/src/main/java/com/techfork/global/config/InitialDataConfig.java b/src/main/java/com/techfork/global/config/InitialDataConfig.java index ac3e791..1ac1de0 100644 --- a/src/main/java/com/techfork/global/config/InitialDataConfig.java +++ b/src/main/java/com/techfork/global/config/InitialDataConfig.java @@ -7,12 +7,14 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; import java.util.List; @Slf4j @Configuration @RequiredArgsConstructor +@Profile({"local", "local-tunnel", "dev"}) public class InitialDataConfig { private final TechBlogRepository techBlogRepository; diff --git a/src/test/java/com/techfork/domain/post/controller/PostControllerIntegrationTest.java b/src/test/java/com/techfork/domain/post/controller/PostControllerIntegrationTest.java index 12d02dd..d2b2ad4 100644 --- a/src/test/java/com/techfork/domain/post/controller/PostControllerIntegrationTest.java +++ b/src/test/java/com/techfork/domain/post/controller/PostControllerIntegrationTest.java @@ -6,16 +6,16 @@ import com.techfork.domain.post.repository.PostRepository; import com.techfork.domain.source.entity.TechBlog; import com.techfork.domain.source.repository.TechBlogRepository; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.transaction.annotation.Transactional; import org.testcontainers.containers.MySQLContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; @@ -37,22 +37,13 @@ */ @SpringBootTest @AutoConfigureMockMvc -@Transactional @Testcontainers +@ActiveProfiles("integrationtest") class PostControllerIntegrationTest { @Container - static MySQLContainer mysql = new MySQLContainer<>("mysql:8.0") - .withDatabaseName("testdb") - .withUsername("test") - .withPassword("test"); - - @DynamicPropertySource - static void configureProperties(DynamicPropertyRegistry registry) { - registry.add("spring.datasource.url", mysql::getJdbcUrl); - registry.add("spring.datasource.username", mysql::getUsername); - registry.add("spring.datasource.password", mysql::getPassword); - } + @ServiceConnection + static MySQLContainer mysql = new MySQLContainer<>("mysql:8.0"); @Autowired private MockMvc mockMvc; @@ -113,6 +104,14 @@ void setUp() { postKeywordRepository.save(keyword3); } + @AfterEach + void tearDown() { + // 테스트 데이터 정리 (외래키 제약조건 순서 고려) + postKeywordRepository.deleteAll(); + postRepository.deleteAll(); + techBlogRepository.deleteAll(); + } + @Test @DisplayName("GET /api/v1/posts/{postId} - 게시글 상세 조회 성공") void getPostDetail_Success() throws Exception { diff --git a/src/test/resources/application-integrationtest.yml b/src/test/resources/application-integrationtest.yml new file mode 100644 index 0000000..b49f6f6 --- /dev/null +++ b/src/test/resources/application-integrationtest.yml @@ -0,0 +1,39 @@ +spring: + jpa: + hibernate: + ddl-auto: create-drop + properties: + hibernate: + format_sql: true + highlight_sql: true + dialect: org.hibernate.dialect.MySQL8Dialect + + batch: + jdbc: + initialize-schema: always + job: + names: '' + + ai: + anthropic: + api-key: test-dummy-key + chat: + options: + model: claude-3-5-haiku-20241022 + temperature: 0.3 + max-tokens: 8192 + openai: + api-key: test-dummy-key + timeout: 60 + chat: + options: + model: gpt-4o-mini + temperature: 0.3 + max-tokens: 8192 + +logging: + level: + com.techfork: DEBUG + org.springframework.batch: INFO + org.hibernate.SQL: DEBUG + org.hibernate.tool.schema: ERROR