用Cactus来测试J2ee应用
2010-03-19 00:00:00 来源:WEB开发网这个servlet在doGet,doPost没有实现任何方法,这个不影响我们测试,我们要测试的只是这些public method. 我们的测试代码如下:
package usersystem.test;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
import usersystem.servlet.*;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Hashtable;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.cactus.Cookie;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.WebResponse;
import javax.ejb.*;
import javax.servlet.*;
import usersystem.*;
public class ManaServletTest extends ServletTestCase{
ManaServlet servlet=new ManaServlet();
public ManaServletTest(String theName) {
super(theName);
}
public void setUp(){
try {
servlet.init() ;
}
catch (ServletException ex) {
ex.printStackTrace() ;
this.fail() ;
}
}
public void tearDown(){
}
public void beginAddUser(WebRequest theRequest)
{
theRequest.addParameter("name", "nameValue");
theRequest.addParameter("pass","passValue") ;
theRequest.addParameter("tel","telValue") ;
theRequest.addParameter("address","addressValue") ;
theRequest.addParameter("email","emailValue");
}
public void testAddUser() throws javax.ejb.EJBException{
servlet.addUser(request,response) ;
}
public void testFindAll(){
java.util.Iterator i=servlet.findAll() ;
//assertEquals(null,i);
boolean ok=false;
while(i.hasNext() ){
if(((User)i.next()).getName().equals("nameValue")) {
ok=true;
};
}
this.assertTrue(ok) ;
}
public void testFindByName() throws javax.ejb.EJBException {
User u=servlet.findByName("nameValue") ;
UserInfo ui=u.getUserInfo() ;
this.assertEquals("email",ui.getEmail()) ;
this.assertEquals("tel",ui.getTel()) ;
this.assertEquals("nameValue",u.getName()) ;
this.assertEquals("passValue",u.getPassword()) ;
}
public void testDel() throws javax.ejb.EJBException {
servlet.delUser("nameValue8") ;
}
public void testDelAll() throws javax.ejb.EJBException {
servlet.delAll() ;
}
public static void main(String[] theArgs)
{
junit.textui.TestRunner.main(new String[]{
ManaServletTest.class.getName()});
}
public static Test suite()
{
return new TestSuite(ManaServletTest.class);
}
}
更多精彩
赞助商链接