New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global onError doesn't called for FakeApplication (tests) #2484
Comments
Actually, that's a feature - let's say you changed the implementation of Your controller tests should test that the right exception is thrown. This is how most people test, and if the It's the same reason why we don't invoke But if you don't want this flexibility, then that's easy to address, simply write your own def routeWithOnError[A](req: Request[A]): Option[Future[SimpleResult]] = {
route(req).map { result =>
result.recoverWith {
case t: Throwable => Global.onError(req, t)
}
}
} |
Thanks @jroper |
You can use something like the following code in java 8:
|
Thanks @zarinfam |
The post is very helpful. I am writing tests (using FakeApplication & FakeRequest) for controllers (for which the global exceptions are handled in the Global.onError() method), in which I want include/cover the execution of {{Global.onError method}}. I am using play framework in java, and missing something rather basic here, and that is how do you obtain the "global" reference in the java example of routeWithOnError given above. @mohsenkashi (or someone) could share a fully functional route(), if the approached worked? |
For test you can write a new Global class and send an instance of it to fakeApplication() method:
or you can get real global of your project using this code:
|
Java 8 version of routeWithOnError for play 2.4+:
|
For play scala 2.4 with an ErrorHandler bound to the application via a GuiceApplicationBuilder, you still need to call it with a helper method.
|
Hi @jroper, Just wanted to inquire whether you haven't changed your position after 3 years. I can find a few counterarguments to your claim that we don't need to test Of course, the approach you suggested - writing tests for the Please tell us what you think of this, and thanks in advance! |
It would be nice to document this lack of custome error handler as part of |
Reproduced on Play 2.2.2
Catch all errors and handle them in Global.scala
Generate exception in action
The problem is that if you call that action in browser, you'll get 400 BAD_REQUEST exactly like it's handled in Global.scala, but you can't cover it with test because this test fails with exception:
@RunWith(classOf[JUnitRunner])
class ApplicationSpec extends Specification {
"Application" should {
"render the index page" in new WithApplication{
val home = route(FakeRequest(GET, "/")).get
status(home) must equalTo(BAD_REQUEST)
}
}
}
The text was updated successfully, but these errors were encountered: