Hi Davor,

Davor Cengija wrote:
> How to unit-test taglibs, possibly without servlet container?
> 
> I'm writing wml-related taglib for struts and my code is craving for
> some unit-tests. I found tagunit but it requires servlet container, it's
> executed in a jsp page etc. I'd like to have automated unit testing on
> each build.
> 
> How does Cactus fit in? I recently discovered it but haven't downloaded
> it yet.

Cactus provides an "in-container" test framework, so it doesn't quite 
meet your requirements. However, it does provide custom ant tasks and 
sample scripts to automate server startup, deployment and server 
shutdown. The result is that the tests take longer to execute, and that 
you need to do a bit of configuration work, but you get a very nice 
framework that also makes sure your taglib works on many containers.

The alternative is to use a mock/dummy-objects approach. See (for 
example) http://www.mockobjects.com/ for mock implementations of many 
servlet/jsp-related classes. This approach has the advantage of having 
faster-executing tests and not requiring a container to be installed. 
OTOH, the mock implementation itself may have bugs or behave slightly 
different than "real" implementations.

Personally, I prefer the Cactus approach, because the "faith" in the 
correctness of your taglib is higher.

A common problem with testing tag libraries is that you either need to 
execute them in a simple JSP page, or simulate the inner workings of the 
JSP engine (lifecycle, synchronization, etc.). For example, if you want 
to test how your tag deals with being reused, you need to know pretty 
precisely how a JSP engine would implement this, within the boundaries 
determined by the JSP spec. Considering the tremendous amount of subtle 
differences between, and bugs in, different servlet containers, the test 
results might be quite far from reality.

I usually do unit tests by calling the taglib directly from the testcase 
(using Cactus), and in addition test the tags in simple JSP pages that 
preferrably generate XML responses that are easily "assertable" by the 
test code (using Cactus + HttpUnit). This latter type of test 
(functional) can of course only be performed against a real servlet 
container.

-- 
Christopher Lenz
/=/ cmlenz at gmx.de


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to