I'm developing a Java 6 EE application and I'm testing my jsp code with another one with a testing version of the function calls and code used in the original one but it seems loose and impractical. Is there a good way to accomplish this kind of testing?
If you haven't read about MVC (model view controller), do so. You shouldn't have code in a JSP, just display. Putting code in JSP is very 1900's.
Seriously though, if there is no code in the JSP, you aren't testing the JSP. You are testing the action/flow. Then you could use HttpUnit or Selenium. The big difference is that Selenium tests from a real browser.
You can also consider using a HTTP unit test framework like HTTPUnit | http://httpunit.sourceforge.net/.
Another important point is to well separate concerns of your application.
E.g. using techniques like TDD (http://en.wikipedia.org/wiki/Test-driven_development), you'll design types for testability.
Types consumed within JSP will be tested in specific unit tests. If this is not possible, you should simulate user -> browser interaction (again, HTTPUnit o similar tool).
I don't think there is a good way to test JSPs, mainly because they were developed before unit testing became a focus of development.
Robert Martin wrote an article several years ago about hacking the JSP compiler so that you can direct, non-container based unit tests. His idea was good, but it was broken with the very next TomCat major release. There's just too much magic going on.
I disagree with the "just don't add code and you won't need to test it" idea. OBVIOUSLY you should not be putting code in the JSP. But nonetheless a complex UI will often have display logic that could be profitably unit tested.
Consider this example:
This code is already well factored: the logic to decide whether we show the admin menu is not in the view. Nonetheless, if there were an easy way to unit test JSPs, then we could write a test to show that the behavior we want actually appears, and it would protect us from a change to the page that accidentally made the admin menu visible when it shouldn't be.
There exists a program (used by whatever application server you are using) that compiles a .jsp file into a .java file. For example, the sun/oracle version jspc.
Once you have the .java that would be produced by the .jsp translation (you might even want to consider using this as part of the build process - precompiling the jsp for performance improvements on the first hit), you can then run tests against it by mocking the request and verifying the response is what you are expecting.
(edit with example:)
The key method for this is the
A trivial hello world jsp:
(test.jsp located within a directory named 'webapp' and also an 'out' directory) When compiled with the command
At this point, its a check to make sure that the JspWriter is called with write or print and the contents of the call are what you expect it to be.
All that said, in an ideal world, one shouldn't have any logic within the jsp - such logic would either be in the controller or taglibs which are tested with other techniques.