Search code examples
javascalaplayframeworkplayframework-2.0specs2

Can I test Java controllers with specs2 in Play Framework 2?


I have an controller that is written in Java and I would like to test it using Scala with specs2. I can test my controller with JUnit and it works fine. But I am unable to test it with specs2. I have followed the documentation and it mentions I should pass a fakeRequest as an parameter. But methods in the Java controller doesn't accept any parameters so I am unable to use this approach.

How can I test it? Only way I could think of would be using same methods as in JUnit but then using specs2 doesn't bring benefits.


Solution

  • Oh, I have figured it out myself.

    I can use helpers from play.test.Helpers then I use them with specs2 matchers and it works as intended.

    import controllers.routes
    import org.specs2.mutable.Specification
    import play.test.Helpers._
    import play.api.test._
    
    class MyControllerSpec extends Specification {
      "My Controller" should {
        "respond with text/plain content type" in new WithApplication {
          val result = callAction(routes.ref.MyController.index(), fakeRequest())
          contentType(result) mustEqual "text/plain"
        }
      }
    }