I’m using httpunit ServletRunner for servlets testing. Now I want to test servlet which uses request attributes (not parameters). Attributes are put to the request by my filter. So for servlet to work properly, there has to be filter installed.
Is there some way to add filter to ServletRunner?
I don’t think that can be done. HttpUnit does not provide setAttribute method for WebRequest, which makes sense, since attributes can only be set from within the container.
However, what you can do (and possibly even should) is to test both individually in separate tests – that filter puts the parameters to the request, and servlet works if parameters are set up correctly. The way to do it would be creating a mock request for the servlet, setting the attributes for that, and running a regular unit test for that. Same approach applies for the filter.
Here’s an example of doing it with a filter:
Mock* objects are from Spring. If your framework does not have them, you can create them by using some mock library, or use the ones that can be seen from Spring sources.