Junit 测试简单例子

Action excute() 方法

public String execute() throws Exception {


try {
context = new InitialContext();
tcmuserRemote = (TSysUserFacadeRemote) context.lookup("TCMS/TSysUserFacade/remote");
} catch (Exception e) {
this.addActionError("can not get the remote service ");
e.printStackTrace();
}

cs = tcmuserRemote.findByUAccountAndUPwd(UAccount, UPwd);


//根据用户类型返回不同字段
if(cs == null){
    this.addActionError("userNo or password not");
    return INPUT;
    }

else if(cs != null)
{
Uid = cs.getUId();
UStatus = cs.getUStatus();
status = cs.getStatus();

logger.info("UStatus is"+UStatus);

UName = cs.getUName();

System.out.println("UStatus "+UStatus);


if (UStatus == null){
        this.addActionError("UStatus is null");
        return "usertypeinput";
        }
    if (UName == null){
        this.addActionError("UName is null");
        return "usernameinput";
    }
    if (status.equals("delete")){
      this.addActionError("status is delete");
      return "delete";
    }
    else if(UStatus.equals("A")){
    String uid = cs.getUAccount();
logger.info("UAccount is :" + uid);
pubSessionInfo(); //put session方法
    return "admin";
            }
   
    else if(UStatus.equals("S") ){
         String uid = cs.getUAccount();
     logger.info("UAccount is :" + uid);
     pubSessionInfo();   //put session方法
         return "student";
            }
   
        else if(UStatus.equals("T")){
    String uid = cs.getUAccount();
logger.info("UAccount is :" + uid);
pubSessionInfo();   //put session方法
    return "teacher";
            }
   
}
    return SUCCESS;

   
}

//put session 方法  pubSessionInfo()
@SuppressWarnings("unchecked")
public final void pubSessionInfo()
{

session.put("UAccount", UAccount);
session.put("UId" ,Uid);
logger.info("*** put session success ! ***");
}






测试代码:


package org.cst.test.User;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cst.action.User.GetAllUser;
import org.cst.action.User.LoginAction;
import org.cst.ejb.user.TSysUser;
import org.cst.ejb.user.TSysUserFacade;
import org.cst.util.testFixture.annotation.DataCreator;

import com.opensymphony.xwork2.ActionProxy;





public class LoginActionTest extends DESTestBase<TSysUserFacade> {

public LoginActionTest() {
super(TSysUserFacade.class);
}

/**
*
*/
ActionProxy proxy;
/**
*
*/
private static final String NAMESPACE = "";



/**
* The action name.
*/
private static final String ACTION_NAME = "login";
/**
*
*/
LoginAction action;


@DataCreator
public final void createData() throws Exception{
System.out.println("the begining of LoginActionTest");
TSysUser user1 = new org.cst.ejb.user.TSysUser("20","admin" , "123","tony","A","active");
TSysUser user2 = new org.cst.ejb.user.TSysUser("21","teacher", "123","jack","T","active");
TSysUser user3 = new org.cst.ejb.user.TSysUser("22","student", "123","grubby","S","active");
TSysUser user4 = new org.cst.ejb.user.TSysUser("23","admin1","123","sky",null,"active");
TSysUser user5 = new org.cst.ejb.user.TSysUser("24","admin2","123",null,"A","active");
TSysUser user6 = new org.cst.ejb.user.TSysUser("25","admin3","123","fly","A","delete");

this.persist(user1);
this.persist(user2);
this.persist(user3);
this.persist(user4);
this.persist(user5);
this.persist(user6);


logger.info("have been persist user");
}

public void testLoginActionAdmin() throws Exception{
     this.proxy = this.getActionProxy(NAMESPACE, ACTION_NAME);
     this.action = this.getAction(proxy, LoginAction.class);
     this.action.setSession(sessionAttributes);
     this.action.setUAccount("admin");
     this.action.setUPwd("123");
     String reply = action.execute();
assertEquals("admin",reply);
}

public void testLoginActionTeacher() throws Exception{
     this.proxy = this.getActionProxy(NAMESPACE, ACTION_NAME);
     this.action = this.getAction(proxy, LoginAction.class);
     this.action.setSession(sessionAttributes);
     this.action.setUAccount("teacher");
     this.action.setUPwd("123");
     String reply = action.execute();
assertEquals("teacher",reply);
}

public void testLoginActionStudent() throws Exception{
     this.proxy = this.getActionProxy(NAMESPACE, ACTION_NAME);
     this.action = this.getAction(proxy, LoginAction.class);
     this.action.setSession(sessionAttributes);
     this.action.setUAccount("student");
     this.action.setUPwd("123");
     String reply = action.execute();
assertEquals("student",reply);
}

public void testLoginActionUNameNull() throws Exception{
     this.proxy = this.getActionProxy(NAMESPACE, ACTION_NAME);
     this.action = this.getAction(proxy, LoginAction.class);
     this.action.setSession(sessionAttributes);
     this.action.setUAccount("admin1");
     this.action.setUPwd("123");
     String reply = action.execute();
assertEquals("usertypeinput",reply);
}

public void testLoginActionUstatusNull() throws Exception{
     this.proxy = this.getActionProxy(NAMESPACE, ACTION_NAME);
     this.action = this.getAction(proxy, LoginAction.class);
     this.action.setSession(sessionAttributes);
     this.action.setUAccount("admin2");
     this.action.setUPwd("123");
     String reply = action.execute();
assertEquals("usernameinput",reply);
}


public void testLoginActionDelete() throws Exception{
     this.proxy = this.getActionProxy(NAMESPACE, ACTION_NAME);
     this.action = this.getAction(proxy, LoginAction.class);
     this.action.setSession(sessionAttributes);
     this.action.setUAccount("admin3");
     this.action.setUPwd("123");
     String reply = action.execute();
assertEquals("delete",reply);
}

public void testLoginActionInput() throws Exception{
this.proxy = this.getActionProxy(NAMESPACE, ACTION_NAME);
this.action = this.getAction(proxy, LoginAction.class);
this.action.setSession(sessionAttributes);
this.action.setUAccount("testerror");
this.action.setUPwd("testerror");
String reply = action.execute();
assertEquals("input",reply);
}

}

猜你喜欢

转载自fsiaonma.iteye.com/blog/952609
今日推荐