Skip to content

Test ‐ TestContainer

Woo Jin Jang edited this page Jun 25, 2025 · 4 revisions
  • 테스트에서 도커 컨테이너를 실행할 수 있는 라이브러리
  • 테스트 실행시 DB를 설정하거나 별도의 프로그램 또는 스크립트를 실행할 필요가 없다.
  • 보다 Production에 가까운 테스트를 만들 수 있으나 테스트 실행 속도가 느려진다는 단점이 있다.

📚 TestContainer 기능

// https://mvnrepository.com/artifact/org.testcontainers/testcontainers
testImplementation group: 'org.testcontainers', name: 'testcontainers', version: '1.21.2'
  • @TestContainers : JUnit5 확장팩으로 테스트 클래스에 @Container를 사용한 필드를 찾아서 컨테이너 라이프 사이클 관련 메서드를 실행해준다.
  • @Container : 인스턴스 필드에 사용하면 모든 테스트마다 컨테이너를 재시작하고, static 필드에 사용하면 클래스 내부 모든 테스트에서 동일한 컨테이너를 재사용한다.
@Testcontainers
@ExtendWith(MockitoExtension.class)
class StudyServiceTest {

	@Mock
	protected StudyRepository studyRepository;

	@Mock
	protected MemberService memberService;

	@Container
	static GenericContainer postgreSQLContainer = new GenericContainer("postgres:latest")
			.withEnv("POSTGRES_DB", "studytest")
			.withEnv("POSTGRES_PASSWORD", "testpass")
			.withExposedPorts(5432);

        // ...
        }
}
public ComposeContainer environment = new ComposeContainer(
    new File("src/test/resources/composev2/compose-test.yml")
)
    .withExposedService("redis-1", REDIS_PORT)
    .withExposedService("db-1", 3306);
  • YAML로 관리하는 도커 컴포즈 파일을 위와 같이 관리를 할 수 있다.

📖 Java

📖 Kotlin

📖 Coroutine

📖 Spring

📖 Spring Security

📖 Spring Batch

📖 Reactive Programming

📖 Database

📖 MySQL

📖 Redis

📖 JPA

📖 QueryDsl

📖 MSA

📖 Kafka

📖 Apache Flink

  • [Apache Flink - Apache Flink Architecture]
  • [Apache Flink - Stream Processing]
  • [Apache Flink - Data Stream API & Window]
  • [Apache Flink - State Management]

📖 HTTP

📖 AWS

📖 Docker

📖 Kubernetes

📖 CI/CD

📖 Nginx

📖 Monitoring🥈

  • [Monitoring - Log Concept]
  • [Monitoring - Log Level & Filter]
  • [Monitoring - Logback]
  • [Monitoring - Log Collection with ELK Stack]
  • [Monitoring - Log Monitoring with Kibana]
  • [Monitoring - Building a Monitoring System with Spring Boot Actuator]
  • [Monitoring - Server Monitoring with Prometheus and Grafana with Discord Alerts]

📖 Test

📖 Effective Java 3/E

📖 Kotlin Academy - Effective Kotlin

📖 Kotlin Academy - 핵심편

📖 스프링으로 시작하는 리액티브 프로그래밍

📖 가상 면접 사례로 배우는 대규모 시스템 설계 기초 1

📖 가상 면접 사례로 배우는 대규모 시스템 설계 기초 2

📖 Clean Code

📖 리팩토링 2판

📖 주니어 백엔드 개발자가 반드시 알아야 할 실무 지식

📖 GraphQL

Clone this wiki locally