IntroductionI was looking for a simple solution to test
@WebFilterservlet filters. The tests that I had used a lot of mocks, but really just left me unsatisfied. It was not a real world production solution. How would they "really" behave in the actual server environment. I had been using Arquillian to do some testing of our CDI layer, and decided to try it on filters.
I looked at the API, and examples and found really nothing. I found one really "bogus" test example from core api tests that left me really wondering if I should bother. As many of you know though, I love a challenge. I tried a number of ideas including making the filter a JSR-316
@ManagedBean. I am not sure what glue I sniffed for that one (it didn't work...), and using
@Inject(that didn't work either... more glue).
What I settled on was using Arquillian Warp and Drone to check the
HttpServletResponseafter the filter processed it. There are a couple of annotations
@AfterServletthat helped me with checking my filters. There are no before and after filter annotations though. That would have made life easier.
Note: The thing to keep in mind with these annotations is that the filtering will have already occurred. You are looking at the post-filter results.
I also wanted to be able to demonstrate another really cool feature called ShrinkWrap, and its Java EE descriptor extensions. This allows you to programmatically create your
web.xmlfile to provide additional flexibility like overriding annotations.
Problem DescriptionWe want to be able to test a
HttpServletResponseheaders. The filter should be tested in isolation, and in a real container to examine the real-world response. The tests should be able to be run on a variety of containers with no modification to the test code.
A SolutionUsing Maven, Arquillian, Warp, Drone, ShrinkWrap, and GlassFish 18.104.22.168 embedded we can accomplish all aspects of the testing requirements. I used GlassFish since it is the Reference Implementation for Java EE 5, EE 6 and EE7, and it is a great application server. The container can be swapped out easily in maven with another profile. In the example code below, I used NetBeans 7.4 Beta to do the development.
The code for the project can be found on BitBucket here: maven-arqullian-filter-test
There are two types of examples included. One set of examples demonstrate simple integration tests that use annotations, and annotation override using
web.xml. The second set of examples demonstrate dynamic
web.xmlgeneration, and overriding. Here are some code snippets from the project.