关于在SSH2整合时,如何使用单元测试
2014-12-15 14:53
197 查看
在SSH2整合时,无论如何我们都要测试我们service层的方法是否正确可言,当测试通过的时候,就可以在Action中使用该方法了。现在我来讲一讲如何使用Junit4 来进行测试。
1,首先要下载一个junit4.jar
下载junit4.jar,然后将其复制到WEB-INF/lib中
下载地址:http://download.csdn.net/detail/zhangkai1992/8261857
2,比如我要测试的是UsersService类中的方法。
首先写UsersService类
public class UsersService implements UsersServiceInter {
public Users checkUsers(Users users){
String hql="from Users where username=? and pwd= ?";
Object[] parameters = {users.getUsername(),users.getPwd()};
List<Users> list = null;
try {
list = this.executeQuery(hql, parameters);
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("查询失败");
}
if(list.size()!=0){
return list.get(0);
}else{
return null;
}
}
public boolean checkUsername(String username){
String hql="from Users where username=?";
Object[] parameters={username};
//Users users = (Users)this.uniqueQuery(hql, parameters);
List<Users> list = null;
try{
list= this.executeQuery(hql, parameters);
}catch(Exception e){
System.out.println("搜索失败");
}
if(list.size()!=0){
return true;
}else{
return false;
}
}
public List<Users> getUsersByPage(int pageNow, int pageSize) {
// TODO Auto-generated method stub
String hql = "from Users order by id";
return this.executeQueryByPage(hql, null, pageNow, pageSize);
}
public int getPageCount(int pageSize) {
// TODO Auto-generated method stub
return this.queryPageCount("select count(*) from Users", null, pageSize);
}
public long getUsersAllRows() {
// TODO Auto-generated method stub
String hql = "select count(*) from Users";
return this.getAllRows(hql);
}
}
3,创建一个专门用于单元测试的开发包,比如com.zk.junit。在此包下创建usersServiceTest类
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zk.domain.Users;
import com.zk.service.interfaces.UsersServiceInter;
public class UsersServiceTest {
private static UsersServiceInter usersService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
usersService = (UsersServiceInter)ac.getBean("usersService");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
@Test
public void checkUsername() throws Exception{
System.out.println(usersService.checkUsername("admin"));
}
@Test
public void checkUsers() throws Exception{
Users users = new Users();
users.setUsername("admin");
users.setPwd("admin");
try{
users = usersService.checkUsers(users);
System.out.println(users.toString());
}catch(Exception e){
System.out.println("异常");
e.printStackTrace();
}
}
}
右击run,以junit单元测试运行,将看到结果如下图所示,表示单元测试试用成功。
其中@BeforeClass类似于初始化,在执行单元测试函数时,先加载。
@Test就是我们需要测试的函数,必须返回public void
1,首先要下载一个junit4.jar
下载junit4.jar,然后将其复制到WEB-INF/lib中
下载地址:http://download.csdn.net/detail/zhangkai1992/8261857
2,比如我要测试的是UsersService类中的方法。
首先写UsersService类
public class UsersService implements UsersServiceInter {
public Users checkUsers(Users users){
String hql="from Users where username=? and pwd= ?";
Object[] parameters = {users.getUsername(),users.getPwd()};
List<Users> list = null;
try {
list = this.executeQuery(hql, parameters);
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("查询失败");
}
if(list.size()!=0){
return list.get(0);
}else{
return null;
}
}
public boolean checkUsername(String username){
String hql="from Users where username=?";
Object[] parameters={username};
//Users users = (Users)this.uniqueQuery(hql, parameters);
List<Users> list = null;
try{
list= this.executeQuery(hql, parameters);
}catch(Exception e){
System.out.println("搜索失败");
}
if(list.size()!=0){
return true;
}else{
return false;
}
}
public List<Users> getUsersByPage(int pageNow, int pageSize) {
// TODO Auto-generated method stub
String hql = "from Users order by id";
return this.executeQueryByPage(hql, null, pageNow, pageSize);
}
public int getPageCount(int pageSize) {
// TODO Auto-generated method stub
return this.queryPageCount("select count(*) from Users", null, pageSize);
}
public long getUsersAllRows() {
// TODO Auto-generated method stub
String hql = "select count(*) from Users";
return this.getAllRows(hql);
}
}
3,创建一个专门用于单元测试的开发包,比如com.zk.junit。在此包下创建usersServiceTest类
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zk.domain.Users;
import com.zk.service.interfaces.UsersServiceInter;
public class UsersServiceTest {
private static UsersServiceInter usersService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
usersService = (UsersServiceInter)ac.getBean("usersService");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
@Test
public void checkUsername() throws Exception{
System.out.println(usersService.checkUsername("admin"));
}
@Test
public void checkUsers() throws Exception{
Users users = new Users();
users.setUsername("admin");
users.setPwd("admin");
try{
users = usersService.checkUsers(users);
System.out.println(users.toString());
}catch(Exception e){
System.out.println("异常");
e.printStackTrace();
}
}
}
右击run,以junit单元测试运行,将看到结果如下图所示,表示单元测试试用成功。
其中@BeforeClass类似于初始化,在执行单元测试函数时,先加载。
@Test就是我们需要测试的函数,必须返回public void
相关文章推荐
- 如何使用单元测试 测试WEB组件方法中的含有Session、Cookie等HttpContext功能调用的方法?
- 关于如何在BCP和Bulk Insert中使用 文本限定符
- CSDN上一篇关于如何有效的使用C#读取文件的文章 很不错的技术文章
- 如何使用单元测试 测试WEB组件方法中的含有Session、Cookie等HttpContext功能调用的方法?[转载]
- 关于.NET程序中使用Excel对象后,如何结束残留的Excel进程
- 关于如何让 JBuilderX 使用中文字体
- 关于在php中如何使用session```Cookies~``
- 关于如何在Blender中使用RenderMan(Pixie Aqsis etc.)渲染的配置
- 关于.NET程序中使用Excel对象后,如何结束残留的Excel进程。
- sqlserver关于函数中如何使用Getdate()(转贴)
- 关于如何使用struts标签输出值为map的list
- 关于如何在testlink里使用fckedit组件来上传图片
- 使用mock object做单元测试,mock object如何插入?
- 关于如何使用XenCode混淆ASP.NET 2.0的代码
- 如何使用CppUnit进行单元测试
- 关于asp.net(c#),webconfig中如何定义一个字符串让所有页面共同使用?
- 关于css中如何使用#和.的总结
- 关于如何使用自定义的结束消息循环的方式 (转载)
- 如何使用CppUnit进行单元测试
- 关于如何使用ADO连接数据库