views:

31

answers:

1
+4  Q: 

Unit testing JSPs

Hi, I would like to ask you what technologies exist out there for creating unit tests for JSPs.

I am already aware of the HtmlUnit/HttpUnit/JWebUnit/Selenium possibilities.

Thank you!

+4  A: 

I would say that testing a web app (be it JSP or other) using its web interface, with one of the above mentioned frameworks is not unit testing in the strict sense. Unit testing means testing small parts of your app (one method, one class) in isolation. With a web framework, you test the web app as a whole, so this is more like system or integration test. Which is not to say it is not useful - on the contrary - just it is better to clarify terminology.

Having said that, if I were to test a JSP, I would most likely be satisfied with a system test of all specific scenarios associated with the JSP, using some of the tools mentioned above.

However, if you really want "classic" unit tests, I guess the closest you can get is compiling your JSP into a servlet class, then calling the servlet methods directly (from e.g. JUnit, using a mocking framework like EasyMock to prepare the Http request, response etc. objects).

Update: IMO the only reason to need unit tests for a JSP is if you have business logic in it. Which, in turn, goes against the separation of UI and business layer. So it is better to redesign the app and move the logic into a separate POJO (if you have the choice) than try to write contrived unit tests in order to test business logic where it doesn't belong.

Péter Török
Yes I know that HtmlUnit and the others are integration tests and not Unit tests. The reason I have mentioned them is that I knew people will mention them as options, and I wanted to save time.But anyway, thanks for your answer :)
Avi Y
@Avi, in the meantime I got a few more thoughts on this, see my update.
Péter Török
+1: That's a wise answer. Put your business logic in something that *is* testable, and keep your JSP simple enough that it comes under the heading of “obviously no bugs” (rather than “no obvious bugs”).
Donal Fellows