user2609980
user2609980

Reputation: 10484

How to not bind to localhost with ScalatestRouteTest

I want to test routes with ScalatestRouteTest as follows:

trait MyRoutes extends Directives {

  self: Api with ExecutionContextProvider =>

  val myRoutes: Route =
    pathPrefix("api") {
      path("") {
        (get & entity(as[MyState])) {
          request => {
            complete(doSomething(request.operation))
          }
        }
      }
    }
  }
}


class RoutesSpec extends WordSpecLike with Api with ScalatestRouteTest 
  with Matchers with MyRoutes with MockitoSugar {

  "The Routes" should {

    "return status code success" in {
      Get() ~> myRoutes ~> check {
        status shouldEqual StatusCodes.Success
      }
    }
  }
}

When running the test I get the runtime error:

Could not run test MyRoutesSpec: org.jboss.netty.channel.ChannelException: Failed to bind to: /127.0.0.1:2552

I don't want to bind to the localhost. How can this be accomplished?

Upvotes: 3

Views: 456

Answers (1)

user2609980
user2609980

Reputation: 10484

The solution was to disable remoting and clustering (this was enabled in a separate configuration file) and use the default provider.

The actor remoting and clustering conflicts with the running application (started for the routing test). They pick up the same configuration and therefore both try to use the same port which conflicts.

The following code was added in trait MyRoutes to make it work:

// Quick hack: use a lazy val so that actor system can "instantiate" it
// in the overridden method in ScalatestRouteTest while the constructor 
// of this class has not yet been called.
lazy val routeTestConfig =
  """
    | akka.actor.provider = "akka.actor.LocalActorRefProvider"
    | persistence.journal.plugin = "akka.persistence.journal.inmem"
  """.stripMargin

override def createActorSystem(): ActorSystem = 
  ActorSystem("RouteTest", ConfigFactory.parseString(routeTestConfig))

Upvotes: 4

Related Questions