I use MVC architectures (Struts, Spring Web) and JSTL for tags, so I do no put 
almost any worthy  to test in a JSP, with the exception of the state of some 
scoped variables, as it showed my previous example. For that kind of tests, I 
prefer to extend ServletTestCase.
Regards,
Pedro
  -----Mensaje original-----
  De: Nagaraj Palanichamy [mailto:[EMAIL PROTECTED]
  Enviado el: jueves, 17 de noviembre de 2005 15:50
  Para: Pedro Nevado
  Asunto: Re: Sample Cactus project-specifically JSP example


  Pedro,
             Still there is some confusion. Whatever we have discussed so far 
is extending ServletTestCsae. Do you have anything like for extending 
JspTestCase?


  Thank you
  Nagaraj P

   
  On 11/17/05, Pedro Nevado <[EMAIL PROTECTED]> wrote: 
     
    Nagaraj, try with what  I have added to the begin() method.
    I hope it helps.
    Pedro
      -----Mensaje original-----
      De: Nagaraj Palanichamy [mailto: [EMAIL PROTECTED]
      Enviado el: jueves, 17 de noviembre de 2005 13:48
      Para: [EMAIL PROTECTED]
      Asunto: Re: Sample Cactus project-specifically JSP example


      Pedro,
                   Greetings!! Now i have got some idea about the 
ServletTestCase.Please clirify the following.

      My MyServletTestCase  looks like:::

      package com.test;

      import javax.servlet.ServletException;
      import junit.framework.Test;
      import org.apache.cactus.ServletTestCase;
      import org.apache.cactus.WebRequest;
      import org.apache.cactus.WebResponse;


      public class MyServletTestCase  extends ServletTestCase {
       
       private TestServlet ts=null;
        

        public TestServletCase(){
         
         super();
         
         System.out.println("Inside the TestServletCase");
        }
        public TestServletCase(String name){
         super(name);
        }
        public TestServletCase(String name,Test t){ 
         super(name,t);
        }

        protected void setUp(){
         ts=new TestServlet();
         try{
            ts.init(this.config);
         }catch(ServletException se){
          System.out.println("Inside the Servletexception se");
          se.printStackTrace ();
         }
        }


        public void begin(WebRequest request){

         request.addParameter("txtName","Nagaraj" [Pedro Nevado] 
,WebRequest.POST_METHOD  );
        } 

        public void testValidSearch(){
         System.err.println("Inside the testValidSearch");
         invokePost();
        }
        public void endValidSearch(WebResponse response){
         assertTrue((response.getText().indexOf("Error:")<0) && 
           (response.getText().indexOf("Search results") >=0 ));
         
        }
        protected void tearDown(){
         ts.destroy();
        }
        
        private void invokePost(){
         try{
          
          
           ts.doPost(this.request, this.response);
         }catch(Exception ex){
          ex.printStackTrace();
         }
        }
      }


      The Actual servlet looks like::::


      package com.test;

      import javax.servlet.ServletConfig;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse ;
      import java.io.IOException;
      import javax.servlet.RequestDispatcher;

      public class TestServlet extends HttpServlet{
       
       public void init(ServletConfig config) throws ServletException{
        super.init(config);
       }
       
       public void doPost(HttpServletRequest req,HttpServletResponse res) 
throws 
         IOException,ServletException{
        System.out.println("Inside the doPost Method");
        String str=req.getParameter("txtName");
        req.setAttribute ("NAME",str);
        System.err.print("str::::"+str);
        
//getServletContext().getRequestDispatcher("/jsp/Results.jsp").forward(req, 
res);
        
       }

      }


      My Client class looks like::::

      class ClientTest{
      public static void main(String args[]){
      TestServletCase ts=new TestServletCase();
       ts.testValidSearch();
      }
      }

      My web.xml looks like:::
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app id="WebApp_ID" version="2.4" xmlns=" 
http://java.sun.com/xml/ns/j2ee " 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd";>
       <display-name>
       UnitTest</display-name>
       <servlet>
        <servlet-name>TestServlet</servlet-name> 
        <servlet-class> com.test.TestServlet</servlet-class>
       </servlet>
       
       <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/Test</url-pattern> 
       </servlet-mapping> 
       <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file> 
        <welcome-file> default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
       </welcome-file-list>
      </web-app> 



      I am getting the Null pointer Exception in doPost() method of TestServlet 
 class(Marked in Red color).It says the incoming request is null.Could you 
please help me i am doing the mistake??
      Then how to invoke the actual test case?? if i will put 
System.out.println() in the testSetUp(),begin() method , it is not executed. 
please help me resolve this one?

      Thank you
      Nagaraj P




Reply via email to