In my project I have this structure
app/
--> common/
--> DefyProductsComponents
--> DefyProductsLoader
--> controllers/
--> HomeController
DefyProductsComponents
package common
import com.softwaremill.macwire.wire
import controllers.{Assets, AssetsComponents, HomeController}
import play.api.ApplicationLoader.Context
import play.api.BuiltInComponentsFromContext
import play.api.routing.Router
import play.filters.HttpFiltersComponents
import router.Routes
import scala.concurrent.Future
class DefyProductsComponents(context: Context)
extends BuiltInComponentsFromContext(context)
with HttpFiltersComponents
with AssetsComponents {
// Controllers
lazy val homeController = wire[HomeController]
// Router
override lazy val assets = wire[Assets]
lazy val prefix: String = "/"
lazy val defyProductsRouter: Router = wire[Routes]
lazy val router: Router = defyProductsRouter
}
DefyProductsLoader
package common
import play.api._
import play.api.ApplicationLoader.Context
class DefyProductsLoader extends ApplicationLoader {
override def load(context: Context): Application = {
LoggerConfigurator(context.environment.classLoader).foreach { _.configure(context.environment) }
new DefyProductsComponents(context).application
}
}
HomeController
package controllers
import play.api.mvc._
class HomeController (val controllerComponents: ControllerComponents) extends BaseController {
def index() = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index("Welcome to Play"))
}
}
I want to setup the test the tests, this is the test/
structure
test/
--> common/
--> DefyProductsServerTest
--> controllers
--> HomeControllerSpec
DefyProductsServerTest
package common
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.components.OneAppPerTestWithComponents
import play.api.{BuiltInComponents, NoHttpFiltersComponents}
class DefyProductsServerTest extends PlaySpec with OneAppPerTestWithComponents {
override def components: BuiltInComponents = new DefyProductsComponents(context) with NoHttpFiltersComponents {
}
}
HomeControllerSpec
package controllers
import common.DefyProductsServerTest
import play.api.test._
import play.api.test.Helpers._
class HomeControllerSpec extends DefyProductsServerTest {
"HomeController GET" should {
"render the index page from the application" in {
val home = homeController.index().apply(FakeRequest(GET, "/"))
status(home) mustBe OK
contentType(home) mustBe Some("text/html")
contentAsString(home) must include ("Welcome to Play")
}
"render the index page from the router" in {
val request = FakeRequest(GET, "/")
val home = route(app, request).get
status(home) mustBe OK
contentType(home) mustBe Some("text/html")
contentAsString(home) must include ("Welcome to Play")
}
}
}
This setup I wrote is not working and I am not sure why in the HomeControllerSpec
homeController is not found. I understand that in the tests all components from DefyProductsComponents
are available and I am able to override
UPDATE: I created a very similar project in github. Just in case someone want to play with it https://github.com/agusgambina/play-scala-base.git
The easiest to do here is:
in DefyProductsServerTest
, change components
into:
override def components: DefyProductsComponents = ...
Then in your test you should be able to do:
val home = new components.homeController.index().apply(FakeRequest(GET, "/"))