test: SqlSessionFactory 테스트 환경 문제 해결 — 전체 112건 PASS
@SpringBootTest 컨텍스트가 @MapperScan(com.ga) 으로 모든 매퍼 MapperFactoryBean 을 만드는데 test 프로파일이 MybatisAutoConfiguration 을 제외해 SqlSessionFactory 가 없어 깨지던 문제. 기존엔 매퍼를 하나씩 @MockBean 으로 막았으나 신규 매퍼 누락 시마다 컨텍스트 로딩 실패. - TestMyBatisConfig: 미접속 PGSimpleDataSource + SqlSessionFactory 제공 → 전 매퍼 빈 생성, 목록 갱신 불필요 - AbstractControllerTest @Import 에 TestMyBatisConfig 추가 - SettleControllerTest hold stub 2인자→3인자 정정(testCompile 실패 해소) - HANDOFF §3-17 추가 + 테스트 상태 서술 정정 검증: ./gradlew test --rerun-tasks → 112건 PASS (ga-common 57 / ga-api 36 / ga-batch 19, 실패 0). DB 무관. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -102,7 +102,7 @@ class SettleControllerTest extends AbstractControllerTest {
|
||||
@Test
|
||||
@DisplayName("PUT /api/settle/{id}/hold - 200 보류 처리")
|
||||
void hold_ok() throws Exception {
|
||||
willDoNothing().given(service).hold(eq(50L), anyString());
|
||||
willDoNothing().given(service).hold(eq(50L), anyString(), any());
|
||||
|
||||
mockMvc.perform(put("/api/settle/50/hold")
|
||||
.with(MockLoginUser.admin())
|
||||
|
||||
@@ -62,7 +62,7 @@ import static org.mockito.Mockito.doAnswer;
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
|
||||
@AutoConfigureMockMvc
|
||||
@ActiveProfiles("test")
|
||||
@Import(TestSecurityConfig.class)
|
||||
@Import({TestSecurityConfig.class, TestMyBatisConfig.class})
|
||||
public abstract class AbstractControllerTest {
|
||||
|
||||
@Autowired protected MockMvc mockMvc;
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.ga.api.support;
|
||||
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.postgresql.ds.PGSimpleDataSource;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 테스트용 MyBatis 인프라.
|
||||
*
|
||||
* application-test.yml 이 MybatisAutoConfiguration / DataSourceAutoConfiguration 을 제외하므로
|
||||
* 컨텍스트에 SqlSessionFactory 가 없다. 그런데 MyBatisConfig 의
|
||||
* {@code @MapperScan(basePackages = "com.ga")} 가 모든 @Mapper 인터페이스의 MapperFactoryBean 을
|
||||
* 생성하고, 이들은 SqlSessionFactory(또는 SqlSessionTemplate)를 요구한다.
|
||||
*
|
||||
* 과거에는 AbstractControllerTest 가 매퍼를 하나씩 @MockBean 으로 막아 이 요구를 회피했으나,
|
||||
* 새 매퍼가 추가될 때마다 목록을 갱신해야 했고 누락 시
|
||||
* "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required" 로 컨텍스트 로딩이 깨졌다.
|
||||
*
|
||||
* 여기서 미접속 DataSource 기반 SqlSessionFactory 를 한 번 제공하면 매퍼 빈이 모두 생성되어
|
||||
* 그 문제가 사라진다. 컨트롤러 테스트는 Service 를 mock 하므로 실제 쿼리는 실행되지 않는다.
|
||||
*/
|
||||
@TestConfiguration
|
||||
public class TestMyBatisConfig {
|
||||
|
||||
/** 실제 연결하지 않는다. SqlSessionFactory 생성과 Mapper 빈 등록 용도로만 존재. */
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
PGSimpleDataSource ds = new PGSimpleDataSource();
|
||||
ds.setUrl("jdbc:postgresql://localhost:5432/test");
|
||||
ds.setUser("test");
|
||||
ds.setPassword("test");
|
||||
return ds;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
|
||||
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
|
||||
factory.setDataSource(dataSource);
|
||||
return factory.getObject();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user