Play 2.4: How do I disable routes file loading during unit tests?
Asked Answered
A

2

13

Background: I am using Play 2.4 (Java) with InjectedRoutesGenerator and a Guice module to configure various dependencies. But during unit tests, the FakeApplication is trying to load all the Controllers from routes file through the injector and some of them are failing due to external dependencies not available in the unit test environment.

How do I disable the default routes file processing during unit tests that extend from play.test.WithApplication? Or how can I replace the default routes with a custom routes file?

I tried to use the play.http.router config option override referenced here, but I get Router not found error with anything I tried. Obviously I am making some mistake, I am not sure where.

I am not quite understanding the link between the my.application.Router and conf/my.application.routes referenced in the config reference. Route files other than routes do not get compiled either.

Arsenide answered 20/7, 2015 at 4:50 Comment(0)
A
25

I am answering my own question here. After spending some more time with Play source code, I figured out the connection between the routes file and the generated Router class. Hope it helps someone else.

Play's route compiler task compiles all files in conf folder ending with .routes as well as the default routes file. Generated class name is always Routes, but the package name depends on the file name. If the file name is routes (the default routes file), compiled class is placed in router package, so the fully qualified class name is router.Routes (which is the default value for play.http.router).

For all other route files, RouteCompiler derives the package name by dropping the .routes from the file name. So for my.test.routes, play.http.router value should be my.test.Routes.

Here is the base class for my tests, with custom router and db configuration elements.

public class MyTestBase extends WithApplication {
    @Override
    protected Application provideApplication() {
        Application application = new GuiceApplicationBuilder()
                .configure("db.default.driver", "org.h2.Driver")
                .configure("db.default.url", "jdbc:h2:mem:play")
                .configure("play.http.router", "my.test.Routes")
                .build();
        return application;
    }
}
Arsenide answered 20/7, 2015 at 18:27 Comment(3)
I spend tons of times till found your post and managed to fix this. You saved my day, thanks!Expulsive
I'm still not getting it. I just have a file called "routes" under "app/conf" (the default routes file). I tried "router.Routes" and it's not found. I didn't quite get what "my.test" maps to. Do you have a file called my.test.route?Demarcate
Yes, I used 'app/conf/my.test.routes' for my tests. What do you mean my 'I tried "router.Routes" and it's not found'? If you are using the default routes file under 'app/conf', you don't need to set play.http.router config var.Arsenide
O
1

If you want to just load no routes at all, here's a trait you could mix in to your test class if you're using Scala, Guice and ScalaTest. This is working with Play 2.5. I've also shown how you could disable filters, since those are related to routing.

I know this is a little different from the ask on Java and Play 2.4, but this might be helpful to people as I got to this question trying to achieve something very similar.

trait DisabledRouting extends PlaySpec with OneAppPerSuite {

  override def fakeApplication(): Application = {
    configureApplication(new GuiceApplicationBuilder()
      .router(Router.empty)
      .configure("play.http.filters" -> "play.api.http.NoHttpFilters"))
      .build()
  }

  /** Override to add additional configuration on top of disabled routing */
  def configureApplication(appBuilder: GuiceApplicationBuilder): GuiceApplicationBuilder = appBuilder

}
Overstay answered 8/5, 2019 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.