• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

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

20.12.29 23:33 작성 조회수 678

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");

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

감사합니다.

Dong Gyu Park님의 프로필

Dong Gyu Park

2022.12.08

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