본문 바로가기

JAVA - Backend/Plugins

Testcontainers (Docker)

https://www.testcontainers.org 

 

Testcontainers

 Testcontainers About Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container. Testcontainers make the following

www.testcontainers.org

테스트에서 도커 컨테이너를 실행할 수 있는 라이브러리

@Testcontainers, @Container

홈페이지에가서 테스트에 필요한 모듈을 의존성 추가하고 사용
# spring.datasource.url=jdbc:tc:postgresql:///studytest
# spring.datasource.dirver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver

static PostgreSQLContainer posgreSQLContainer = new PostgreSQLContainer().withDatabaseName(*"studytest");

@BeforeAll
static void beforeAll(){
    postgreSQLContainer.start()
    System.out.println(postgresSQLContainer.getJdbcUrl());
}

@AfterAll
static void afterAll(){
    postgreSQLContainer.stop()
}

또는

@Testcontainers 를 클래스에 static 변수 필드에 @Container를 사용해도 같은 효과
@BeforeEach
studyReposotyr.deleteAll()로 정리

제공기능

컨테이너 만들기 : New GenericContainer(String imageName)
네트워크
withExposedPorts
getMappedPort
환경 변수 설정 : withEnv(key, value)
명령어 실행 : withCommanid(String cmd)
사용할 준비가 됐는지 확인하기
waitingFor(Wait)
Wait.forHttp(String url)
Wait.forLogMessage(String message)
로그 살펴보기 
getLogs()
followOutput()

컨테이너 정보를 스프링 테이스트에서 참조하기

@ContextConfiguration

Docker Compose

여러컨테이너를 일련의 관계로 사용해야 할 경우 컨테이너들의 관계를 설정하고 관리

static DockerComposeContainer composeContainer = 
    new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"));

http://testcontainers.org/modules/docker_compose/ 

 

Docker Compose Module - Testcontainers

 Docker Compose Module Benefits Similar to generic containers support, it's also possible to run a bespoke set of services specified in a docker-compose.yml file. This is intended to be useful on projects where Docker Compose is already used in dev or oth

www.testcontainers.org

http://github.com/palantir/docker-compose-rule (대체제)

 

palantir/docker-compose-rule

A JUnit rule to manage docker containers using docker-compose - palantir/docker-compose-rule

github.com