Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 코테
- 배열
- 프로그래머스
- Java
- GIT
- 포트폴리오
- LV01
- Lv.0
- JPA
- 알고리즘
- LV.02
- LV03
- LV0
- LV1
- spring boot
- 일정관리 프로젝트
- 연습문제
- Join
- 일정관리프로젝트
- SQL
- LV02
- S3
- Til
- docker
- 데이터 베이스
- 이것이 자바다
- Redis
- CoffiesVol.02
- mysql
- 디자인 패턴
Archives
- Today
- Total
코드 저장소.
TestContainer를 활용한 테스트 코드 작성하기. 본문
목차
1.TestContainer?
2.테스트 컨테이너의 장단점
3.작성 방법
1.TestContainer?
테스트컨테이너란 코드로 도커 컨테이너를 제어하여 통합테스트를 도와주는 라이브러리입니다. 로컬에 설치된 도커데몬과 연동되어 테스트코드가 실행되기 전 코드를 통해 해당 테스트를 위한 일회성 컨테이너를 생성하고 테스트 수행 후 컨테이너를 삭제합니다. 테스트컨테이너를 응용하면 테스트 때 뿐만 아니라 런타임 중에도 컨테이너를 생성하고 활용할 수 있습니다.
2.각 테스트 컨테이너의 장단점
장점
- 실제 환경과 유사한 테스트 환경 제공
- 다양한 데이터베이스 및 서비스 제공
- 테스트가 끝나면 자동으로 컨테이너 정리
- JUnit 4, JUnit 5 지원
단점
- 테스트 실행 속도가 느려질 수 있음
- 컨테이너를 실행하는 데 시간이 소요되므로, 단순한 단위 테스트보다 실행 시간이 길어질 수 있습니다.
- 리소스 사용량 증가
- 컨테이너를 실행할 때마다 메모리와 CPU 리소스를 소비하므로, 테스트 환경에서 리소스 제약이 있는 경우 주의가 필요합니다.
3.작성 방법
테스트컨테이너를 스프링부트와 같이 사용을 해보기 위해서는 다음과 같이 진행을 해주시면 됩니다.
3-1.라이브러리 의존성 추가.
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
testImplementation 'org.testcontainers:testcontainers' // TC 의존성
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.testcontainers:mysql'
testImplementation 'org.testcontainers:jdbc'
3-2.테스트 리소스 디렉토리에 있는 설정 파일 작성하기.
application-test.properties
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.jdbc-url=jdbc:tc:mysql:latest://localhost/container
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=create
application.properties
spring.profiles.active=test
3-3. 테스트 코드를 작성
@SpringBootTest
@Testcontainers
@ActiveProfiles("test")
public class BoardRepositoryTest {
@Container
@ServiceConnection
static MySQLContainer<?> container = new MySQLContainer<>("mysql:latest")
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");
@Autowired
private BoardRepository boardRepository;
@Test
void boardCreatTest() {
Board board = Board
.builder()
.title("test title")
.contents("test contents")
.author("test author")
.build();
boardRepository.save(board);
List<Board> list = boardRepository.findAll();
assertThat(board).isNotNull();
assertThat(list).isNotNull();
}
@Test
void selectBoardTest() {
Board board = boardRepository.findById(1L).get();
assertThat(board).isNotNull();
assertThat(board.getTitle()).isEqualTo("test title");
assertThat(board.getContents()).isEqualTo("test contents");
assertThat(board.getAuthor()).isEqualTo("test author");
}
}
'백엔드 > Spring' 카테고리의 다른 글
스프링에서 사용되는 @Transaction의 작동 원리. (0) | 2025.02.06 |
---|---|
Spring Batch?? (0) | 2024.05.12 |
Aop?? (관점지향 프로그래밍) (0) | 2024.05.04 |
스프링 DI (의존성 주입) (0) | 2024.04.15 |
Spring Security의 구조 및 흐름 (0) | 2023.09.21 |