what are the best practices to unit test java servlets? By the way: this is a topic in which I have some dificulty: how do you unit test your java servlets?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The most important thing to do is to try and extract everything from the servlets that isn’t directly related to servlet behaviour.
This immediately makes testing core functionality a lot easier. By doing this you immediately have a set of components not tied to the container and testable without the pain of running and interfacing to a container (besides making them more reusable). Some thought should be given to architecture and the appropriate layering of components – e.g. components returning object structures rather than displayable fragments, not making use of HttpRequests directly but some request marshalling structure etc.
The majority of your tests (dependent on your system structure and complexity) can be tested normally. Additional servlet-focused tests can be built using (say) Apache Cactus to sanity check functionality. Beyond that you may wish to investigate in-browser solutions such as Selenium.
(Note: This approach works for most GUI environments – e.g. Swing)