Sunday, September 25, 2011

Cactus For UnitTesting JEE Code

Cactus is a simple test framework for unit testing server-side Java code (Servlets, EJBs, Tag libs, ...) from the Jakarta Project. The intent of Cactus is to lower the cost of writing tests for server-side code. It uses JUnit and extends it. Cactus implements an in-container strategy, meaning that tests are executed inside the container.

The Steps to Configure Cactus are below,

1) the library files it sets up are junit-3.8.1.jar, aspectjrt-1.1.1.jar, cactus-1.6.1.jar, commons-httpclient-2.0.jar, and commons-logging-1.0.3.jar.
As for the web.xml file, it will use the following configuration:

 
<servlet>
	<servlet-name>ServletTestRunner</servlet-name>
	<servlet-class>
		org.apache.cactus.server.runner.ServletTestRunner
	</servlet-class>
</servlet>

<servlet>
	<servlet-name>ServletRedirector</servlet-name>
	<servlet-class>
		org.apache.cactus.server.ServletTestRedirector
	</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>ServletTestRunner</servlet-name>
	<url-pattern>/ServletTestRunner</url-pattern>
</servlet-mapping>

<servlet-mapping>
	<servlet-name>ServletRedirector</servlet-name>
	<url-pattern>/ServletRedirector</url-pattern>
</servlet-mapping>
 
 
2) Take following java classes in default or any other pkg.
                       this is our Test Class
TestSampleServlet.java
--------------------------
import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;


public class TestSampleServlet extends ServletTestCase
{
    public TestSampleServlet(String theName)
    {
        super(theName);
    }

    public static Test suite()
    {
        return new TestSuite(TestSampleServlet.class);
    }
  
   public void beginSaveToSessionOK(WebRequest webRequest)
	    {
             //here we set i/p parameters for testing method

	        webRequest.addParameter("ipparam", "90");
	    }
   
	    public void testSaveToSessionOK()
	    {
	        
	    	 SampleServlet servlet = new SampleServlet();
	         servlet.saveToSession(request);
	         assertEquals(90, session.getAttribute("testAttribute"));
	    }
}	
 
Clearly observe methods,
public void beginXXX(WebRequest theRequest)
public void testXXX()
public static Test suite()
public void endXXX(WebResponse theResponse) 
 (Note : Once see Methods,here   http://jakarta.apache.org/cactus/writing/howto_testcase.html )

 
 
SampleServlet
------------------
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;


public class SampleServlet extends HttpServlet
{
    public void saveToSession(HttpServletRequest request)
    {
       //here we get all  the i/p parameters that we set in beginxxxx(-) in above class
    	String testparam = request.getParameter("ipparam");
 
       //Businessservice to test
    	PatientVitalsBusinessService patbservice = new PatientVitalsBusinessService();
    	PatientVitals pat=null;
        try{
        	 System.out.println("in  ----- try block ----:::::");
       //here findByID() Business Method to test.
		    pat = patbservice.findByID(Integer.parseInt(request.getParameter("ipparam")));
		    System.out.println("patttttttttttttttttt id:::::"+pat.getPavId()+"-"+pat.getPavCreatedbyTxt()+"-"+pat.getPavTmpType()+"-"+pat.getPavWtUnit());
		    request.getSession().setAttribute("testAttribute", pat.getPavId()); 
        }catch(Exception e){
                	e.printStackTrace();
        }
    }
}	

3)Test the Application.
write this url in browser, 
http://localhost:8080/(your WebApplicationName)CactusJakartaExample/ServletTestRunner?suite=TestSampleServlet


Here the steps are,
* our web application call ServletTestRunner class which is configured in web.xml
*this automatically calls TestSampleServlet class 
* Here beginxxxx() executed and calls testxxxxx()
* so finally our Business method calls and output come as XML format.
 

No comments:

Post a Comment