Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapped port can only be obtained after the container is started #137

Closed
mithunatri opened this issue Nov 5, 2020 · 2 comments
Closed

Mapped port can only be obtained after the container is started #137

mithunatri opened this issue Nov 5, 2020 · 2 comments

Comments

@mithunatri
Copy link

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.

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
    )
  }
}
@mithunatri
Copy link
Author

To add, this works without any issue with the new API spec.

Closing this issue as it is exactly same as #134 (but for PostgresSQLContainer).

@markarasev
Copy link
Contributor

markarasev commented Dec 2, 2020

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants