This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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;
}
}
 
We noticed he had no friends. So we gave him this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!