배경
- 멀티 모듈로 구성되어 있다: s3 접근하는 data-s3 모듈, 스프링 배치 batch 모듈
- batch 모듈에서 s3 접근이 필요한 로직들은 data-s3 모듈에 들어있다.
하고싶은 것
data-s3 모듈에서 테스트용 빈을 구현해 batch모듈에서도 쓰고 싶다.
- S3Mock을 사용한 테스트용 S3Client
지금까지는
batch 모듈에서 직접 구현함
- batch 모듈에서 S3Mock 디펜던시 주입 (
io.findify:s3mock_2.12) - batch 모듈에서 S3MockConfig 빈 생성
→ S3MockConfig 는 s3와 관련된 클래스이니 data-s3 모듈에서 생성하고, batch 모듈에 공유하여 사용하고 싶은 것
첫 번째 방법 - testFixtures 플러그인 사용
- (장점) batch 모듈에서
testImplementation으로 S3Mock 디펜던시 주입 안 함 (data-s3 모듈의 의존성 그대로 사용) - (단점) 사용법이 상대적으로 번거로움
(data-s3 모듈) build.gradle 추가
plugins {
id 'java-test-fixtures' // 플러그인 추가
}
bootJar {enabled = false}
jar {enabled = true}
dependencies {
implementation("io.awspring.cloud:spring-cloud-aws-starter-s3:3.0.5")
testImplementation("io.findify:s3mock_2.12:0.2.4")
// testFixture용 디펜던시 주입
testFixturesApi("io.findify:s3mock_2.12:0.2.4")
testFixturesImplementation('org.springframework.boot:spring-boot-starter-test')
testFixturesImplementation("org.projectlombok:lombok:${lombokVersion}")
testFixturesAnnotationProcessor('org.projectlombok:lombok')
}
java-test-fixtures플러그인 추가 필요testFixturesImplementation가 아닌testFixturesApi로 한 이유- testFixturesApi("io.findify:s3mock_2.12:0.2.4") ← 이 부분
- Implementation으로 하면 batch모듈에서 S3Client 클래스 없다고 뜨기 때문
- 롬복, @TestConfiguration 사용 위해 나머지 Fixture 디펜던시 주입해줌
(data-s3 모듈) testFixtures 폴더 생성

폴더 직접 생성하고 모듈화하고 싶은 클래스 집어넣기
(batch모듈) build.gradle 추가
testImplementation(testFixtures(project(":elsboo-data:elsboo-data-s3")))
(batch모듈) 테스트 클래스에서 사용하면 됨
@Import(S3MockConfig.class)
...
@Autowired
private S3Client s3Client;
...
두 번째 방법 - sourceSets.test.output
- (장점) testFixtures 보다 사용 방법은 더 간단
- (단점) batch 모듈에도 s3 관련
testImplementation주입 필요
(data-s3 모듈) build.gradle 추가
configurations {
testOutput
}
dependencies {
...
testOutput sourceSets.test.output
}
configurations : testOutput 이라는 커스텀 명령어를 쓰겠다.
testOutput sourceSets.test.output : build/classes/java/test 클래스들을 testOutput이라는 가상의 폴더에 담는다.

(batch모듈) build.gradle 추가
testImplementation(project(path: ':elsboo-data:elsboo-data-s3', configuration: 'testOutput'))
testImplementation("io.awspring.cloud:spring-cloud-aws-starter-s3:3.0.5")
testImplementation("io.findify:s3mock_2.12:0.2.4")
configuration: testOutput에 담긴 테스트 클래스들을 쓸 수 있게 선언- s3 관련 디펜던시 주입 필요 (안 넣으면 batch 모듈에서 못 읽음)
(batch모듈) 테스트 클래스에서 사용하면 됨
@Import(S3MockConfig.class)
...
@Autowired
private S3Client s3Client;
...
글 읽어주셔서 언제나 감사합니다. 좋은 피드백, 개선 피드백 너무나도 환영합니다.
'SearchDeveloper > Java' 카테고리의 다른 글
| [Webflux] nested flatMap 지옥 해결하기 (0) | 2025.03.15 |
|---|---|
| ListenableFuture 의 Callback Hell 해결하기 (0) | 2023.05.03 |
| [JPA] 영속성 컨텍스트(Persistence Context) : 엔티티 관리 공간 (0) | 2022.10.03 |
| Java 애플리케이션 메모리 누수(Memory leak) 잡기 - jstat, MAT (0) | 2022.10.02 |
| Java 에서 테스트용 도커 컨테이너 띄우는 법 : TestContainer (4) | 2022.06.21 |