Skip to content

Instantly share code, notes, and snippets.

@joseraya
Created October 3, 2013 18:57
Show Gist options
  • Save joseraya/6815099 to your computer and use it in GitHub Desktop.
Save joseraya/6815099 to your computer and use it in GitHub Desktop.
Play framework mocked application: All the controllers are mocks and do nothing. Useful for testing routes. Defines a mocekdApp method that should be used like: "wathever" in mockedapp { //here you can call route(FakeRequest(...)) and use Mockito.verify or mustInvoke[Controllerclass].method(params) }
import play.api.test.Helpers._
import play.api.test._
import play.api.mvc.Result
import play.api.libs.json.JsValue
import play.api.libs.json.Json
import play.api.GlobalSettings
import org.mockito.stubbing.OngoingStubbing
import scala.reflect.ClassTag
import org.specs2.mock.Mockito
import play.api.mvc.Call
trait MockedApp {
class MockingGlobal extends GlobalSettings {
var controller: Any = None
override def getControllerInstance[A](clazz: Class[A]): A = {
controller = org.mockito.Mockito.mock(clazz)
controller.asInstanceOf[A]
}
}
val global = new MockingGlobal
val application = FakeApplication(withGlobal = Some(global), additionalConfiguration = inMemoryDatabase(), withoutPlugins = List("play.modules.swagger.SwaggerPlugin"))
def mockedapp(block: => Unit) = {
running(application) {
block
}
}
def mustInvoke[A]: A = org.mockito.Mockito.verify(global.controller.asInstanceOf[A])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment