Specs2 Web

After some consideration, I think that the web testing components of Specs2 Spring Web are useful enough on their own, with the Spring testing code a nice icing on the cake (an yes, the pun was intended).

The minefield branch in the Git repository (https://github.com/janm399/specs2-spring/tree/minefield) contains the initial refactoring; I will complete it over the next few days. My aim is to separate the code that performs the HTTP operations into two traits: the DispatcherServletDispatcher, which will access the Spring-based web application to perform the “web” requests, and RealDispatcher, which will make real HTTP requests. The traits contain methods that represent the HTTP methods (get, post, and so on).

class RealWebSpec extends org.specs2.mutable.Specification 
  with org.specs2.web.RealDispatcher {

  "some spec" in {
    val wo = Xhtml(get)("http://www.app-under-test.com/index.html")
    // here wo is instance of WebObject[XhtmlWebObjectBody]
    wo.body >>! ("#header") must_== ("Header One")
  }
}

Notice that this specification uses the original Specs2 Specification and makes the HTTP requests using the real I/O (courtesy of the RealDispatcher).
The old Specs2 Spring Web code remains almost the same as the code in the master branch, except that you now have to mixin the DispatcherServletDispatcher trait:

class RealWebSpec extends org.specs2.spring.web.Specification 
  with org.specs2.spring.web.DispatcherServletDispatcher {

  "some spec" in {
    val wo = Xhtml(get)("/index.html")
    // here wo is instance of ModelAndViewAwareWebObject[XhtmlWebObjectBody]
    wo.body >>! ("#header") must_== ("Header One") ^
    wo.model[User].getFullName must_== ("Jan")
  }
}
This entry was posted in Jan's Blog and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>