Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unit Testing JSP

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to unit test JSP? And what is the value of doing so? I mean the view could be changing all the time and the human eye is always the best tool to test a view. Is it still valuable to unit test JSP?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is lots of value in testing JSP.

Not least in the time and attention it takes to test every page in even a slightly complex application. Remember, you really ought to be testing every page every time you make a change, to make sure you haven't accidentally broken anything.

The important thing to get to grips with is which bits of the generated pages are important, and which are just decoration. Then write some tests (I use HTTPUnit, for example) to drive the deployed web application and check the navigation betwen pages, and the bits you have identified as important.

Being choosy with what you test should help you avoid the problem of having to change the tests for every small look-and-feel modification.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that what Frank is describing (using HttpUnit) is not unit testing but functional testing. It's valuable, yes, but not unit testing.

You might want to check out these two pages from our wiki:
KindsOfTests
UnitTestingWebApplications
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what Frank is describing (using HttpUnit) is not unit testing but functional testing. It's valuable, yes, but not unit testing.

Hmm. I'm not sure the distinction is that clear cut.

Certainly some web interface tests are obviously "functional" - the ones which exercise aggregate behaviour of the whole system.

However, I still think that there can also be cases in which testing a web interface component such as a JSP counts as unit testing.

How about deploying a JSP into a stub or mock web application?

I don't personally like JSP very much (I generally prefer templating solutions, as they are easier to unit test in isolation) but when I find myself working with them I usually create and deploy a simple, minimal, web application to a server which notices file changes (my habit is to use Resin). No database, external communications, hefty business logic or anything else to slow it down.

Then, to unit test each JSP, my test script copies it into place in the "stub" web application, then accesses it with HTTPUnit to verify that it works as expected.

Sure, such tests are usually slower than regular in-memory unit tests, but that's a problem with the JSP compile-on-demand philosophy more than with the idea of unit testing a web interface as such.

Once I have such tests in place, I can then TDD the necessary changes.

Does that make any sense.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank, what you describe makes very much sense, and...

How about deploying a JSP into a stub or mock web application?

...this is indeed very close to what I would call "unit testing" a JavaServer Page. In fact, it's somewhat similar to what I set out to do with the JspTest project.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic