[2.4.x] Is it possible to set a custom Play ApplicationLoader in scalatest specs?

245 views
Skip to first unread message

Loic

unread,
Aug 28, 2015, 5:22:39 AM8/28/15
to play-framework
Hi,

I'm using an injected router (at compile time) and custom ApplicationLoader : 

class FakeApplicationLoader extends ApplicationLoader {
 
def load(context: Context) = {
   
new FakeApplicationComponents(context).application // <- contains mocks
 
}
}


With Specs2 I can use this FakeApplicationLoader as follows : 

"check that every workse" in new WithDepsApplication{
      val home
= route(FakeRequest(GET, "/").get
      status
(home) must equalTo(OK)
      contentType
(submit) must beSome.which(_ == "text/html")
      contentAsString
(submit) must contain ("Hello")
}



WithDepsApplication is defined like this :

import play.api.test.WithApplicationLoader
class WithDepsApplication extends WithApplicationLoader(new FakeApplicationLoader)


Is it possible to do the same thing with scalatest?

The documentation says that I can define a FakeApplication object like :
implicit override lazy val app: FakeApplication = FakeApplication()

But it seems that I can't pass an ApplicationLoader parameter to FakeApplication.


Please note that I can still use "in new WithDepsApplication" with scalatest but it requires to add play-specs2 to build.sbt, which does not seem very clean.

Thanks,

Loïc

Marius Soutier

unread,
Aug 28, 2015, 9:06:47 AM8/28/15
to play-fr...@googlegroups.com
In ScalaTest+Play 1.4.0-M4 you can mix in OneAppPerSuite and override `app`, or alternatively OneAppPerTest and override `newAppForTest`, to instantiate the app from your FakeLoader.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/cbec40d8-5dff-46a3-bb9e-fac61d42772a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yann Simon

unread,
Aug 28, 2015, 9:13:43 AM8/28/15
to play-fr...@googlegroups.com

Marius Soutier

unread,
Aug 28, 2015, 9:31:34 AM8/28/15
to play-fr...@googlegroups.com
Yeah, that's just like Loïc's example, no?

Loic

unread,
Aug 28, 2015, 10:03:56 AM8/28/15
to play-framework
Hi, thanks to both of you for your answers!

I've tried something similar to Yann example : 

object WithDepsApplication {


 
def app = {
    val appLoader
= new FakeApplicationLoader
    val context
= ApplicationLoader.createContext(
     
new Environment(new File("."), ApplicationLoader.getClass.getClassLoader, Mode.Test)
   
)


    appLoader
.load(context)
 
}
}

Then in my test :

implicit val application = WithDepsApplication.app

But as my app is reading some configuration keys, I have this kind of error : 

*** FAILED ***
[info]   java.lang.RuntimeException: There is no started application

I guess because my app is not a FakeApplication.
It's working WithApplicationLoader from play-specs2 helper but I'm not sure to understand why...

Thanks

Loïc

Loic

unread,
Aug 28, 2015, 11:32:27 AM8/28/15
to play-framework
By the way, as my app is not a FakeApplication, would it be possible to use it for tests needing an inMemoryDatabase parameter ?

Thanks

Marius Soutier

unread,
Aug 28, 2015, 4:16:37 PM8/28/15
to play-fr...@googlegroups.com
This works for me:

class ApplicationTest extends PlaySpec with OneAppPerSuite {
override implicit lazy val app: api.Application = {
val appLoader = new FakeAppLoader

val context = ApplicationLoader.createContext(
new Environment(new File("."), ApplicationLoader.getClass.getClassLoader, Mode.Test)
)
appLoader.load(context)
}

  "check that everything works" in {
val play = route(FakeRequest(GET, "/play")).get
status(play) mustEqual OK
contentType(play) mustEqual Some("text/html")
contentAsString(play) must include("Play Framework")
}

}

PS Will blog about it soon…


Loic

unread,
Aug 31, 2015, 4:33:57 AM8/31/15
to play-framework
Indeed with  OneAppPerSuite it's working, thanks :)

 
PS Will blog about it soon…


Great  news, if you have an example working with an inMemoryDatabase parameter in the application it would be awesome.
Currently I have this exception : "java.lang.NoSuchMethodException: play.api.db.DBApi.<init>()"

Loïc
Reply all
Reply to author
Forward
0 new messages