인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

안소현님의 프로필 이미지
안소현

작성한 질문수

더 자바, 애플리케이션을 테스트하는 다양한 방법

IllegalStateException : Mapped port can only be obtained after the container is started 오류

작성

·

898

0

'TestContainers  기능 살펴보기' 강좌에서  GenericContainer를 만들고 실행하는데

계속 Mapped port can only be obtained after the container is started 오류가 납니다.

POSTGRES_PASSWORD 세팅도 해보고, POSTGRES_HOST_AUTH_METHOD =trust 로 수정도 해보고,

버전도 모두 확인했는데 어떤 이슈인지 잘 모르겠습니다 ...

해당 코드: 

https://github.com/ahnzhx/TIL/blob/master/src/test/java/com/java/tddTheJava/study/StudyServiceTest2.java

에러는 postgreSQLContainer.getMappedPort(5432) 이 부분에서 납니다. 

static class ContainerPropertyInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>{

@Override
public void initialize(ConfigurableApplicationContext context) {
TestPropertyValues.of("container.port="+postgreSQLContainer.getMappedPort(5432))
.applyTo(context.getEnvironment());

}
}

어떻게 해결해야할지 알려주시면 다시 해보겠습니다!!

감사합니다.

답변 1

3

백기선님의 프로필 이미지
백기선
지식공유자

TestContainers 의존성 버전을 1.15.1로 변경해 주세요.

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.15.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.15.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>jdbc</artifactId>
<version>1.15.1</version>
<scope>compile</scope>
</dependency>

그리고 컨테이너 선언한 부분에 @Container 애노테이션도 추가해 주시구요.

    @Container

    static GenericContainer postgreSQLContainer = new GenericContainer("postgres")

            .withExposedPorts(5432)

            .withEnv("POSTGRES_PASSWORD", "studytest");

//            .withEnv("POSTGRES_DB", "studyTest");

이렇게 하면 동작할 겁니다.

감사합니다.

이 문제 혹시 원인이 뭔지 알려주실수 있나요?

안소현님의 프로필 이미지
안소현

작성한 질문수

질문하기