SSH框架的测试

下载junit包:http://sourceforge.net/projects/junit/?source=directory
将junit.jar放到项目lib文件夹中。

一、测试Servcie层
1、添加SpringTestBase类
package com.hsinghsu.testSSH;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:applicationContext*.xml"})
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class SpringTestBase {

}
2、测试Service层
新建UserServiceTest类 ,该类需继承SpringTestBase类
package com.hsinghsu.testSSH.service;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.hsinghsu.testSSH.SpringTestBase;

public class UserServiceTest extends SpringTestBase
{
	@Autowired
	private UserService userService;

	@Test
	public void testConfig() 
	{
		assertNotNull(userService);
	    assertTrue(userService != null);
	}

	@Test
	public void testLogin()
	{
		assertTrue(userService.login("hsing", "hsu"));
	}
}
二、测试action
添加struts2-junit-plugin.jar包。
新建Action测试类UserLoginActionTest.java,该类需要继承StrutsSpringTestCase类,该类在struts2-junit-plugin.jar包中。
package com.hsinghsu.testSSH.action;

import org.apache.struts2.StrutsSpringTestCase;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.junit.Test;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionProxy;

public class UserLoginActionTest extends StrutsSpringTestCase{

	//测试actionMapping
	@Test
	public void testActionMapping()
	{
		// 测试返回
		ActionMapping mapping = getActionMapping("/action/user/userLogin.action");
		// 返回不为空
		assertNotNull(mapping);
		// 返回的命名空间等于
		assertEquals("/action/user", mapping.getNamespace());
		assertEquals("userLogin", mapping.getName());
	}
	
	//测试Action方法一:通过ActionProxy进行执行action
	@Test
	public void testActionProxy() throws Exception 
	{
		request.setParameter("username", "hsing");//提交参数,参数名username
	    request.setParameter("password", "hsu");//提交参数,参数名password
	        
	    ActionProxy proxy = getActionProxy("/action/user/userLogin.action");
		assertNotNull(proxy);
	
		UserLoginAction action = (UserLoginAction) proxy.getAction();
		assertNotNull(action);
	
		String result = proxy.execute();//执行action
		assertEquals(Action.SUCCESS, result);//action返回success结果
	
		String address = action.getAddress();//获取action执行结果
		assertEquals("test address SUCCESS", address);//action返回success结果
	}
	
	//测试Action方法二:通过executeAction函数进行执行action
	@Test
	public void testUserLoginAction() throws Exception
	{
		request.setParameter("username", "hsing");//提交参数,参数名username
        request.setParameter("password", "hsu");//提交参数,参数名password
        executeAction("/action/user/userLogin.action");//执行action
        String address = (String) findValueAfterExecute("address");//获取执行结果
        assertEquals("test address SUCCESS", address);
	}
}
三、附加代码:
UserServiceImpl.java代码如下:
package com.hsinghsu.testSSH.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsinghsu.testSSH.dao.UserDao;
import com.hsinghsu.testSSH.model.User;
import com.hsinghsu.testSSH.service.UserService;

@Service(value = "userService")
public class UserServiceImpl implements UserService{

	@Autowired
 	private UserDao userDao;  
 
 	//  private final static Logger logger = Logger.getLogger(UserServiceImpl.class);
 	private Logger logger = Logger.getLogger(this.getClass().getName());
 
	 public boolean login(String name, String password) 
	 {  
		 logger.info("--UserServiceImpl login method name:"+name+" password:"+password);
	     User user = userDao.getUserByName(name);  
	     
	     if(user!=null)
	     {
	         if(password.equals(user.getPwd()))
	         {
	             return true;  
	         }
	     }
	     return false;  
	 }  
}
UserLoginAction.java代码如下:
package com.hsinghsu.testSSH.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.hsinghsu.testSSH.service.UserService;

@Controller
@Scope("prototype")
@Component("configpagerAction")
@ParentPackage("json-default")
@Namespace(value = "/action/user")
public class UserLoginAction extends BaseAction {

	private static final long serialVersionUID = -877192845581603852L;

	private String username;  
	private String password;
	private String address;

	@Resource(name = "userService")
    protected UserService userService;

	@Override
    @Action(value = "userLogin", results = {@Result(name = "success", type = "json")})
    public String execute() throws Exception 
    {
		System.out.println("userLogin username:"+username+" password:"+password);

		if(userService.login(username,password))  
		{
			address = "test address SUCCESS" ;
		}
        return SUCCESS;
    }

	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
}





你可能感兴趣的:(SSH框架的测试)