Announcement Announcement Module
Collapse
No announcement yet.
how to test a jsp in isolation? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to test a jsp in isolation?

    hello everyone,
    i was wondering my self: how could I test the content of a jsp (whose fields display values returned by a controller) ? I don't want to test which values are returned by the controller (i do that in the controller tests), but more likely testing that given some xpaths to be checked against the rendered jsp, some elements are there, containing given values, and so on.

    any one already experienced this need willing to share his/her experience?

    Thanks,
    Valerio

  • #2
    Originally posted by ervalerio
    hello everyone,
    i was wondering my self: how could I test the content of a jsp (whose fields display values returned by a controller) ? I don't want to test which values are returned by the controller (i do that in the controller tests), but more likely testing that given some xpaths to be checked against the rendered jsp, some elements are there, containing given values, and so on.

    any one already experienced this need willing to share his/her experience?

    Thanks,
    Valerio
    In short; you cannot easily test JSPs in isolation. The only sensible thing to do is deploy your web app with known data and check the rendered page. You could possibly use Jetty, or another lightweight servlet container to aid this, but it is all quite painful.

    You basically need to either look at incontainer testing (cactus etc.) or use a "proper" rendering engine like freeMarker or velocity, both of which have very little costs in terms of startup and rendition.

    Comment


    • #3
      hi, thanks for the quick answer.
      are you saying that using other/different view technologies (as those suggested), it'd be easier to test the pages ? Or: could I test the resulting pages out of the container (which is what I'd like to do).
      Having heavy startup or rendition costs during tests is not an issue, in my case.

      thanks,valerio

      Comment


      • #4
        Originally posted by ervalerio
        are you saying that using other/different view technologies (as those suggested), it'd be easier to test the pages ?
        Absolutely. The problem with JSPs is that they require a servlet container to "execute" them, freemarker/velocity also require these containers, however they are trivial to start, and very very quick. I predominantly use freemarker, but same applies to velocity; they are both extremely easy to unit test.

        Checkout http://freemarker.sourceforge.net/do...iguration.html for more info on freemarker, and http://jakarta.apache.org/velocity/index.html for velocity.

        Essentially both require you to instantiate the engine, populate a Map (the Model in ModelAndView) and then ask the engine to process a template (the View).

        Originally posted by ervalerio
        Or: could I test the resulting pages out of the container (which is what I'd like to do).
        Having heavy startup or rendition costs during tests is not an issue, in my case.
        I would still recommend checking out velocity/freemarker because they are (IMO) must more powerful rendering engines than JSP

        Comment


        • #5
          i know velocity (i used it for a completely different goal - dynamic pojo generation from xml description of provided attributes, via bytecode generation... -) and I like it very much. I'll give it a try asap.

          Thanks for your suggestions,
          Valerio

          Comment


          • #6
            No problems.

            I think others have commented that development on Velocity has stopped, but I haven't looked myself.

            Actually it would have been easier for me to verify that then the time it took to type this message

            Comment


            • #7
              cactus test for a JSP page

              If you decide to go with Cactus, then the attached file may help to get you started.

              I don't know XPath, so I'm not sure if the file will help, but it demonstrates how you can get a DOM view of the HTML content.

              Good luck.

              Comment


              • #8
                Originally posted by yatesco
                No problems.

                I think others have commented that development on Velocity has stopped, but I haven't looked myself.

                Actually it would have been easier for me to verify that then the time it took to type this message
                I did that for you ;-)

                Nope, the development of velocity hasn't been stopped. A new major release (1.5) can be expected this year, I think.


                Best

                Oliver

                Comment


                • #9
                  I'm still evalutating the proposed solutions, thanks everyone.
                  Since Velocity hasn't stopped its development, I'll give it also a chance

                  Comment

                  Working...
                  X