[SOLVED] Reverse Route Difference in Unit Tests since upgrading to 2.8.2

Hi, since upgrading to 2.8.2, we have tests failing whenever a reverse route is calculated.

Here’s a simple test that passes using play-framework 2.8.1 but fails in 2.8.2:

package routes

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import org.scalatestplus.play.guice.GuiceOneServerPerSuite

class HelloWorldRouteSpec extends AnyWordSpec with Matchers with GuiceOneServerPerSuite {
  "Reverse route" should {
    "be full path" in {
      controllers.routes.HelloWorldController.helloWorld() shouldBe "/hello/world"
    }
  }
}

In 2.8.2 the reverse route is returned as just "/world" instead of "/hello/world". Looking at the diffs in GitHub between 2.8.1 and 2.8.2, I can’t figure out what has caused ths.


Other relevant files:

routes:

->      /hello              hello.Routes

hello.Routes:

GET   /world           controllers.HelloWorldController.helloWorld

HelloWorldController:

package controllers

import javax.inject.{Inject, Singleton}
import play.api.mvc._
import scala.concurrent.Future

@Singleton
class HelloWorldController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
  def helloWorld: Action[AnyContent] = Action.async { implicit request => Future.successful(Ok("Hello, World")) }
}

Any explanations appreciated!

Please see https://github.com/playframework/playframework.com/pull/383#issuecomment-633745346 and the further comments and also the code changes there.
You have to initialize the Router by injecting it, to make sure the prefix gets set, something like inject[play.api.routing.Router]
Hope that helps.

1 Like

Thank you Matthias. We’ve been able to get our tests to pass again just by adding app.injector.instanceOf[play.api.routing.Router] to our test mixins.

1 Like

Perfect! I know this is a bit of an unfortunate side effect in the Play 2.8.2 release, which still needs documentation: https://github.com/playframework/playframework/issues/10311

It only effects tests however, where, like in your case, tests only test parts of your routes (when the main router doesn’t get initialized). When starting the app in dev or prod mode everything should work fine because the main router always gets initalized anyway.