aspose file tools*
The moose likes Testing and the fly likes Unit Testing JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Unit Testing JSP" Watch "Unit Testing JSP" New topic
Author

Unit Testing JSP

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
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

Joined: Jan 07, 1999
Posts: 6920
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.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Unit Testing JSP