ITN Dev 07-22
fix: MattermostRestClient 생성자 모호성으로 인한 Spring 빈 생성 실패 수정
생성자가 2개(공개 1-인자, 패키지 전용 2-인자)로 늘어나며 Spring의
단일 생성자 자동 선택 규칙이 더 이상 적용되지 않아, 기본 생성자를
찾지 못해 컨텍스트 리프레시 단계에서 BeanCreationException으로
애플리케이션 기동이 실패하고 있었다. 공개 1-인자 생성자에
@Autowired를 명시해 Spring이 이를 명확히 선택하도록 고쳤다.

Spring 컨텍스트를 실제로 로딩해 MattermostClient 빈을 검증하는
테스트가 없어 이 회귀를 잡지 못했으므로, AbstractDbTest(신규
공통 베이스)와 이를 상속하는 MattermostRestClientSpringWiringTest를
추가해 재발을 방지한다.
@6f984d4895958b243c14e199d2b6346822eb28a8
src/main/java/kr/itn/itnhub/mattermost/MattermostRestClient.java
--- src/main/java/kr/itn/itnhub/mattermost/MattermostRestClient.java
+++ src/main/java/kr/itn/itnhub/mattermost/MattermostRestClient.java
@@ -2,6 +2,7 @@
 
 import com.fasterxml.jackson.databind.JsonNode;
 import kr.itn.itnhub.config.MattermostProperties;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Component;
 import org.springframework.web.client.RestClient;
@@ -17,6 +18,7 @@
     private final RestClient rest;
     private final String teamId;
 
+    @Autowired
     public MattermostRestClient(MattermostProperties props) {
         this(props, RestClient.builder());
     }
 
src/test/java/kr/itn/itnhub/AbstractDbTest.java (added)
+++ src/test/java/kr/itn/itnhub/AbstractDbTest.java
@@ -0,0 +1,34 @@
+package kr.itn.itnhub;
+
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
+import org.springframework.test.context.DynamicPropertyRegistry;
+import org.springframework.test.context.DynamicPropertySource;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+/**
+ * Spring 컨텍스트를 실제로 로딩해야 하는 테스트의 공통 베이스.
+ *
+ * <p>Testcontainers PostgreSQL 컨테이너를 띄우고, 애플리케이션 구동에 필요한
+ * {@code mattermost.*} / {@code app.admin.*} 필수 프로퍼티를 더미 값으로 채워
+ * 컨텍스트 리프레시가 실제 외부 시스템 없이도 성공하도록 한다.</p>
+ */
+@SpringBootTest
+@Testcontainers
+public abstract class AbstractDbTest {
+
+    @Container
+    @ServiceConnection
+    static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16");
+
+    @DynamicPropertySource
+    static void secrets(DynamicPropertyRegistry registry) {
+        registry.add("app.admin.username", () -> "admin");
+        registry.add("app.admin.password", () -> "test-password");
+        registry.add("mattermost.base-url", () -> "http://localhost:1");
+        registry.add("mattermost.token", () -> "test-token");
+        registry.add("mattermost.team-id", () -> "test-team");
+    }
+}
 
src/test/java/kr/itn/itnhub/mattermost/MattermostRestClientSpringWiringTest.java (added)
+++ src/test/java/kr/itn/itnhub/mattermost/MattermostRestClientSpringWiringTest.java
@@ -0,0 +1,28 @@
+package kr.itn.itnhub.mattermost;
+
+import kr.itn.itnhub.AbstractDbTest;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * {@link MattermostRestClient}가 Spring 컨테이너에 의해 실제로 생성될 수 있는지 검증한다.
+ *
+ * <p>{@link MattermostRestClientTest}는 {@code new}로 직접 생성하기 때문에 생성자가
+ * 여러 개일 때 Spring이 어떤 생성자를 선택할지(혹은 선택하지 못해 컨텍스트 로딩에
+ * 실패하는지)를 전혀 검증하지 못한다. 이 테스트는 실제 ApplicationContext를 띄워
+ * 빈 생성 단계에서 생성자 모호성으로 인한 회귀를 잡아낸다.</p>
+ */
+class MattermostRestClientSpringWiringTest extends AbstractDbTest {
+
+    @Autowired
+    private MattermostClient mattermostClient;
+
+    @Test
+    void MattermostClient_빈이_Spring에_의해_정상_생성된다() {
+        assertThat(mattermostClient)
+                .isNotNull()
+                .isInstanceOf(MattermostRestClient.class);
+    }
+}
Add a comment
List