• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Test cases for Action Class  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to generate test cases for Action Classes which contains getters and setters and an execute method containing asession management statements?
Is it possible using JUnit 4?


import java.io.IOException;
import java.sql.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.cts.deligate.DBConnection;
import com.cts.deligate.Logger_util;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.logging.Logger;

public class AddUser extends ActionSupport {
String fname,lname,uname,pwd,email,addr;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}


public String execute() {
String res="fail";
int flag=0;
DBConnection db=new DBConnection();
Connection con;
PreparedStatement pst;
ResultSet rs,rs1;
Statement st,st1;
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();



try {

con=db.getConn();


st=con.createStatement();
st1=con.createStatement();
rs=st.executeQuery("select * from user_pass where username='"+uname+"'");
rs1=st1.executeQuery("select * from user_request where username='"+uname+"'");
if(rs.next()||rs1.next())
{
Logger_util.logger("Start Check Availabilty", 1);
session.setAttribute("fname",fname);
session.setAttribute("uname",uname);
session.setAttribute("ename",email);
session.setAttribute("lname",lname);
session.setAttribute("addr",addr);
String err="<font color=red>User Name already Exists..</font>";
session.setAttribute("err",err);
Logger_util.logger("End Check Availabilty", 1);
return SUCCESS;

}
else{
pst=con.prepareStatement("insert into user_request values(?,?,?,?,?,?,?) ");
int reqid=0;

rs=st.executeQuery("select REQUEST_ID.nextval from dual");
rs.next();
reqid=rs.getInt(1);
Logger_util.logger("Insert User "+reqid+","+uname+","+pwd+","+fname+","+lname+","+email+","+addr, 1);
pst.setInt(1, reqid);
pst.setString(2,uname);
pst.setString(3,pwd);
pst.setString(4,email);
pst.setString(5,fname);
pst.setString(6,lname);
pst.setString(7,addr);
flag=pst.executeUpdate();
pst.execute("commit");
Logger_util.logger("My Flag Value : "+flag, 1);
if(flag==0)
{
res="fail";
return res;
}
else
{
session.setAttribute("msg", uname+" have registered successfully");
res=SUCCESS;
return res;
}
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!