开发者

Test-driven development for JSPs specifically

I've been writing test-driven code even be开发者_Python百科fore understanding what TDD actually was. Calling functions and classes without having its implementation helps me understand and build my application in a much faster and efficient way. So I'm very used the process of writing code->compiling it->seeing it fail->fixing it by building its implementation.

This process is a bit harder for the Web. Specifically JSPs. When I compile my Java classes, all it's fine, I can see the compile errors. However seeing errors in the JSPs require me to open a browser and call that specific JSP.

Is there a way to avoid this process and show me the JSP compile errors without actually loading up a browser?


Sure. You can pre-compile JSP. There is even ant task that does it. Please refer to link: http://ant.apache.org/manual/Tasks/jspc.html

But I think that this is only first step. This will allow you to see compilation errors. I think that you wanted more, i.e. unit tests. I believe that tools like Jakarta Cactus (or others) may help you.

BTW recently I found the following resource that enumerates a huge number of java testing tools.


I typically don't test JSPs directly. It's generally a good idea to keep as little logic in your JSPs as possible, and if you're JSPs only contain a few <c:out> tags then there's not really too much to test. However, if you do have a fair amount of logic within them then what I would do is extract that logic into a custom tag, which you can test drive pretty easily.


Compiling is the easy part, and I think AlexR's answer handles it fine.

Testing and test-driving JSP is difficult, as testing it really requires deploying to a web container or simulating (mocking) this, and running through a browser or something mimicking a browser.

Cactus can help with in-container testing. Selenium can as well.

Or you can mock the environment. If you're using Spring, it has some good support for doing so.

But the best way to deal with the difficulty of testing and test-driving JSP is to stop using JSP altogether, or to at least minimize the logic in your JSP code by avoiding scriptlets.

Since a JSP is just a servlet in disguise, it's always possible to write web applications without JSP, and there are options using frameworks like Wicket and Tapestry or template engines like Velocity to make Java web application development without JSP pretty easy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜