You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use PostgreSQLContainer to perform some integration tests. To try out the library, I am replicating the examples provided in the readme, but I keep running into this error.
Mapped port can only be obtained after the container is started
java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
at org.testcontainers.shaded.com.google.common.base.Preconditions.checkState(Preconditions.java:174)
at org.testcontainers.containers.ContainerState.getMappedPort(ContainerState.java:125)
at org.testcontainers.containers.PostgreSQLContainer.getJdbcUrl(PostgreSQLContainer.java:62)
at com.dimafeng.testcontainers.JdbcDatabaseContainer$class.jdbcUrl(JdbcDatabaseContainer.scala:11)
at com.dimafeng.testcontainers.PostgreSQLContainer.jdbcUrl(PostgreSQLContainer.scala:5)
at org.example.PostgreSQLSpec$$anonfun$1.apply$mcV$sp(PostgreSQLSpec.scala:15)
at org.example.PostgreSQLSpec$$anonfun$1.apply(PostgreSQLSpec.scala:12)
at org.example.PostgreSQLSpec$$anonfun$1.apply(PostgreSQLSpec.scala:12)
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
The logs indicate that the container has started and Postgres is waiting for requests.
I did some further debugging and found that the issue is essentially a Precheck in ContainerState (clearly indicated by the stack trace above) to check the containerId before getting the mapped ports. Where is this containerId set and most importantly, what I am doing wrong?
Using Scala 2.11 with Maven.
Code :
class PostgreSQLSpec extends FlatSpec with ForEachTestContainer {
override def container =
PostgreSQLContainer()
it should "do something" in {
Class.forName(container.driverClassName)
val connection = DriverManager.getConnection(
container.jdbcUrl,
container.username,
container.password
)
}
}
The text was updated successfully, but these errors were encountered:
In my case changing override def container = PostgreSQLContainer() by an override val solved the problem (actually the examples use a val but IntelliJ overrides with a def by default).
markarasev
added a commit
to markarasev/testcontainers-scala
that referenced
this issue
Dec 2, 2020
Overriding container with a `def` seems to be a common mistake (I actually did it, see testcontainers#137). Hope this note will lead to less people experiencing it while not growing too much the "Quick Start" section.
Hi,
I am trying to use PostgreSQLContainer to perform some integration tests. To try out the library, I am replicating the examples provided in the readme, but I keep running into this error.
The logs indicate that the container has started and Postgres is waiting for requests.
I did some further debugging and found that the issue is essentially a Precheck in ContainerState (clearly indicated by the stack trace above) to check the containerId before getting the mapped ports. Where is this containerId set and most importantly, what I am doing wrong?
Using Scala 2.11 with Maven.
Code :
The text was updated successfully, but these errors were encountered: