testing output stream of doGet failed

"214060 - Shou Jiesong" <[email protected]>
Newsgroups gmane.comp.jakarta.cactus.user
Message-ID <FA6F7D6E5D6BF34695A7407F4745AF9A391F07@BLRKECMSG13.ad.infosys.com>
Hi,

I creates a servlet with a doGet() method.

public void doGet(HttpServletRequest request,

        HttpServletResponse response) throws ServletException,
IOException {

        HttpSession session = request.getSession();



        Integer count = (Integer)session.getAttribute("Count");

        if (count == null)

            count = new Integer(0);

        count = new Integer(count.intValue() + 1);

        session.setAttribute("Count", count);

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();



        out.print("<html><head><title>Testing Servlets</title>" +

                "</title></head><body><h1>Count is " + count +

                "</h1></body></html>");

        out.close();

       // System.out.println("doget");

}

Everytime doget() is called, count will increase by 1.



In my test case class, I want to test the output stream of the servlet.

         public void testDoGet() throws ServletException, IOException{

                   SessionTrackingServlet servlet = new
SessionTrackingServlet();

                   servlet.doGet(request, response);

                   System.out.println(session.getAttribute("Count"));

                   servlet.doGet(request, response);

                   System.out.println(session.getAttribute("Count"));

                  

        

         }

        

         public void endDoGet(WebResponse response){

                   String expected = "<html><head><title>Testing
Servlets</title>" +

        "</title></head><body><h1>Count is " + "2" +

        "</h1></body></html>";

                   assertEquals(expected, response.getText());

         }



I observed that the test failed. I called doGet() twice and it was
suppose to increase count to 2. it does in testDoGet() since I could see
the printed result; it was 2.

But in endDoGet(), the count was always 1 whatever how many times
doGet() was called in testDoGet().



Thanks & Regards,

Jason



**************** CAUTION - Disclaimer *****************
This e-mail contains PRIVILEGED AND CONFIDENTIAL INFORMATION intended solely for the use of the addressee(s). If you are not the intended recipient, please notify the sender by e-mail and delete the original message. Further, you are not to copy, disclose, or distribute this e-mail or its contents to any other person and any such actions are unlawful. This e-mail may contain viruses. Infosys has taken every reasonable precaution to minimize this risk, but is not liable for any damage you may sustain as a result of any virus in this e-mail. You should carry out your own virus checks before opening the e-mail or attachment. Infosys reserves the right to monitor and review the content of all messages sent to or from this e-mail address. Messages sent to or from this e-mail address may be stored on the Infosys e-mail system.
***INFOSYS******** End of Disclaimer ********INFOSYS***
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.