Skip to content

Commit

Permalink
Use random port in tests (restore behaviour from Play 2.8.x)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurz committed Oct 6, 2022
1 parent 8ae5451 commit cfb517b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.scalatestplus.play
import org.scalatest._
import play.api.Application
import play.api.test._
import play.core.server.ServerConfig

/**
* Trait that provides a new `Application` and running `TestServer` instance per ScalaTest `Suite`.
Expand Down Expand Up @@ -143,7 +144,11 @@ trait BaseOneServerPerSuite extends TestSuiteMixin with ServerProvider { this: T
implicit lazy val app: Application = fakeApplication()

protected implicit lazy val runningServer: RunningServer =
DefaultTestServerFactory.start(app)
new DefaultTestServerFactory() {
// Restoring the previous behaviour before https://github.com/playframework/playframework/pull/11173 was merged
// Also see https://github.com/playframework/playframework/issues/5473
protected override def serverConfig(app: Application): ServerConfig = super.serverConfig(app).copy(port = Some(0))
}.start(app)

/**
* Invokes `start` on a new `TestServer` created with the `Application` provided by `app` and the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.scalatestplus.play
import play.api.Application
import play.api.test._
import org.scalatest._
import play.core.server.ServerConfig

/**
* Trait that provides a new `Application` and running `TestServer` instance for each test executed in a ScalaTest `Suite`.
Expand Down Expand Up @@ -107,7 +108,11 @@ trait BaseOneServerPerTest extends TestSuiteMixin with ServerProvider { this: Te
def newAppForTest(testData: TestData): Application = fakeApplication()

protected def newServerForTest(app: Application, testData: TestData): RunningServer =
DefaultTestServerFactory.start(app)
new DefaultTestServerFactory() {
// Restoring the previous behaviour before https://github.com/playframework/playframework/pull/11173 was merged
// Also see https://github.com/playframework/playframework/issues/5473
protected override def serverConfig(app: Application): ServerConfig = super.serverConfig(app).copy(port = Some(0))
}.start(app)

/**
* Creates new `Application` and running `TestServer` instances before executing each test, and
Expand Down

0 comments on commit cfb517b

Please sign in to comment.