common 패키지의 추상 클래스 공유하기
💡패키지 전역으로 사용될 TestContainerSupport.java 코드
package com.hubEleven.common.test;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.mysql.MySQLContainer;
import org.testcontainers.utility.DockerImageName;
public abstract class TestContainerSupport {
private static final DockerImageName MYSQL_IMAGE = DockerImageName.parse("mysql:8.0.44");
private static final MySQLContainer MYSQL_CONTAINER;
static {
MYSQL_CONTAINER = new MySQLContainer(MYSQL_IMAGE)
.withDatabaseName("testdb")
.withReuse(true); // 컨테이너 재사용
MYSQL_CONTAINER.start();
}
@DynamicPropertySource
public static void overrideProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.driver-class-name",
MYSQL_CONTAINER::getDriverClassName);
registry.add("spring.datasource.url",
MYSQL_CONTAINER::getJdbcUrl);
registry.add("spring.datasource.username",
MYSQL_CONTAINER::getUsername);
registry.add("spring.datasource.password",
MYSQL_CONTAINER::getPassword);
}
}
💡패키지 path

💡build.gradle - dependencies 추가
// Junit Jupiter
testImplementation "org.junit.jupiter:junit-jupiter:5.8.1"
// Testcontainers
testImplementation "org.testcontainers:testcontainers:2.0.2"
testImplementation "org.testcontainers:testcontainers-junit-jupiter:2.0.2"
testImplementation "org.testcontainers:testcontainers-mysql:2.0.2"
💡테스트 코드 공유 설정 추가
// 테스트 코드 공유 설정 추가
configurations {
testOutput.extendsFrom testImplementation // "testOutput"이라는 새 설정 만들기
}
task testJar(type: Jar) {
from sourceSets.test.output // test 폴더의 코드를
archiveClassifier = 'test' // common-test.jar 로 만들기
}
artifacts {
testOutput testJar // testOutput에 이 jar 등록
}
각 서비스에서 활용하기
💡build.gradle
// 내부 common 모듈 사용
implementation project(':common')
testImplementation(project(path: ':common', configuration: 'testOutput'))
- common이 Jitpack으로 배포되어 있기 때문에 일단 common 모듈을 의존성에 추가해준다.
💡테스트 코드 사용 예시
@SpringBootTest
@Testcontainers
class StockServiceImplTest extends TestContainerSupport {
...
}
- 각 서비스의 테스트 코드에서 common 패키지의 TestContainerSupport 추상 클래스를 상속해서 사용한다.
참고 자료
1) Testcontainer 공식 문서 1
https://java.testcontainers.org/quickstart/junit_5_quickstart/
JUnit 5 Quickstart - Testcontainers for Java
JUnit 5 Quickstart It's easy to add Testcontainers to your project - let's walk through a quick example to see how. Let's imagine we have a simple program that has a dependency on Redis, and we want to add some tests for it. In our imaginary program, there
java.testcontainers.org
2) Testcontainer 공식 문서 2
https://java.testcontainers.org/modules/databases/mysql/
MySQL Module - Testcontainers for Java
MySQL Module Testcontainers module for MySQL Usage example You can start a MySQL container instance from any Java application by using: MySQLContainer mysql = new MySQLContainer("mysql:8.0.36") See Database containers for documentation and usage that is co
java.testcontainers.org
'Projects > HubEleven' 카테고리의 다른 글
| [리팩토링] Stock 도메인 리팩토링 (2) (0) | 2025.12.27 |
|---|---|
| [리팩토링] Stock 도메인 리팩토링 (1) (0) | 2025.12.18 |
| [리팩토링] 서비스 코드 주석 제거, 메서드명 변경 (0) | 2025.12.17 |
| [동시성 처리] 실제 DB와 동일한 환경에서 테스트하기 (0) | 2025.12.09 |
| [동시성 처리] 재고 감소 통합 테스트 코드 1 (초안) (0) | 2025.12.08 |