当前位置: 首页 » 文章 » 软件开发 » 开发向导

StrutsSpringTestCase很好用

作者: yang   发布时间: 2014-01-09 17:08:00
StrutsSpringTestCase很好用
StrutsSpringTestCase很好用

package com.wudimei.user;

import com.opensymphony.xwork2.ActionProxy;
import org.apache.struts2.StrutsSpringTestCase;
import org.junit.Test;

/**
 *
 * @author rong
 */
public class UserAction2JUnitTest extends StrutsSpringTestCase {

    public UserAction2JUnitTest() {
    }

    @Test
    public void testRegister() throws Exception {

        ActionProxy proxy = null;
        UserAction act = null;

        request.setParameter("user.username", "ddd");
        request.setParameter("user.password", "333");
        request.setParameter("user.email", "yaqy@qq.com");
        request.setParameter("user.gender", "1");
        request.setParameter("user.realname", "杨庆荣");
        request.setParameter("password2", "");

        proxy = getActionProxy("/user/perform_register.do");
        act = (UserAction) proxy.getAction();

        String result = proxy.execute();
        System.out.println(result);
         
        assertEquals("register", result);

    }
}

 

比起我自己写的土办法测试要好多了,我写的是这样的:

 

package com.wudimei.user;


import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class UserActionJUnitTest {
    
    public UserActionJUnitTest() {
    }
    
    @BeforeClass
    public static void setUpClass() {
    }
    
    @AfterClass
    public static void tearDownClass() {
    }
    
    @Before
    public void setUp() {
    }
    
    @After
    public void tearDown() {
    }

    @Test
    public void testPerform_register() throws Exception {
        
        BeanFactory beanFactory;
        beanFactory = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        UserDao userDao;
        userDao = (UserDao)beanFactory.getBean("userDao");
               
        UserService userService = (UserService) beanFactory.getBean("userService");
        userService.setUserDao(userDao);
        
        UserAction act;
        act = new UserAction();
        act.setUserService(userService);
       
        
        UserTable user;
        user = new UserTable();
        user.setUsername("yaqy12345");
        user.setPassword("123456");
        user.setEmail("yaqy2qq.com");
        user.setGender(1);
        user.setRealname("yang");
        act.setUser(user);
        
        act.setPassword2("123456");
        
        String res = act.perform_register();
        
        System.out.println( "res:" + res + " ,msg:" +act.getMsg() );
        assertTrue( res.equals("register") );
    }

    
}

 

 

评论

Name(名字):*
Email:*
QQ:*
*
(*are required,带*号的要求输入。)


分类
   关于我们
   最新动态
   软件开发
     案例分析
     软件下载
     软件供应
     开发向导
     wudimei开源