scala - Test HttpErrorHandler with scalates/specs2 in Play Framework 2.4.2 -


i have implemented own httperrorhander in play framework 2.4.2 , functions well, want able test "fake actions" intentionally throw exceptions. have tried in scalatest , specs2

import play.api.http.httperrorhandler import play.api.mvc._ import play.api.mvc.results._ import scala.concurrent._  class myerrorhandler extends httperrorhandler {    def onclienterror(request: requestheader, statuscode: int, message: string) = {     future.successful(       status(statuscode)("a client error occurred: " + message)    )   }    def onservererror(request: requestheader, exception: throwable) = {     future.successful(       internalservererror("a server error occurred: " + exception.getmessage)     )   } } 

i tried far following tests. try debug code, never entering methods. methods of play.api.http.defaulthttperrorhandler neither executed.

object throwablecontrollerspec extends playspecification results {    "example page" should {       "throwerroraction should valid" in {        val controller = new testcontroller()        val result: future[result] = controller.exceptionaction().apply(fakerequest())        //val bodytext: string = contentasstring(result)        status(result) mustequal internal_server_error        //bodytext must startingwith "a server error occurred:"     }   } } 

the action-method in testcontroller.exceptionaction looks:

def exceptionaction() = action {   if (true)     throw new exception("error")   else     ok("") } 

the second try:

class applicationspec extends specification {   "application" should {    "sent 500 on server error" in new withapplication {    route(fakerequest(get, "/exception")) must besome.which(status(_) == internal_server_error)    }    } } 

and route /exception

get        /exception               controllers.testcontroller.exceptionaction 

i added in application.conf play.http.errorhandler. said, working, not able test it. test fails exception given in exceptionaction.

thank in advance

if using specs2, try this

await(controller.exceptionaction()(fakerequest())) must throwa[throwable] // or error want test against 

see these links.

  1. https://twitter.github.io/scala_school/specs.html
  2. http://www.scalatest.org/user_guide/using_assertions (for scalatest)
  3. http://www.scalatest.org/getting_started_with_fun_suite

hope helps!


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -