StrutsTestCase配置的常见问题及其多级Action的处理方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/simon_world/article/details/41291125

StrutsTestCase配置的常见问题及其多级Action的处理方式

一:写本文的原因

          StrusTestCase是比较常见使用的测试方法,网上的教程很多。但是为什么要写这篇文章呢?首先网上的教程版本就2-3种,基本大家都相互copy,而且教程比较陈旧。第二,遇到的不少问题网上的很多人都没给出可用的解决方法。下文会给出自己遇到的一些问题和具体的解决方法。

二:StrutsTestCase的基本原理

         这里简单介绍一下StrutsTestCase的基本原理。StrutsTestCase是Junit的扩展类,主要用于对MVC工程的测试。下面的教程用于纯Struts2工程。StrutsTestCase是一个用于测试Struts行为的基于Junit的测试框架。如果你使用Struts,那么你会注意到它可以提供给你一种容易而有效的方式来测试你的应用程序的Struts行为类。说的明白一点就是用来测试一个具体的Action,同时自己模拟来自网页的输入信息,然后运行一下测试类的excute,得到的结果的和你期望的值进行对比用来测试你的项目有没有问题。至于更加详细的原理大家可以百度找一些资料,本文的重点不在这里。

三:StrutsTestCase的常见问题

其实这里才是写本文的目的所在,这里分享几个我遇到的几个比较坑的问题。
     第一个问题就是我引入了struts2-Junit-Plugin和一些Spring的一些jar包,发现各种类找不到,也就是未发现jar包,可我明明引入了jar包了啊?经过一段时间终于发现了问题的所在,原来我工程的struts2的核心jar包有问题。在那个版本的struts2的jar包发现了StrutsTestCase这个类,但是这个类里面没有任何函数。但是eclipse提醒我们引入类时默认的是struts2核心jar里面提供的StrutsTestCase类。我也很纳闷为何原本提供了这个接口但是不提供相应的测试函数呢?好吧,这时我们只好重新换struts2的核心包了。笔者用的是最新版本的核心包,这个版本是没有这个问题的:http://struts.apache.org/download.cgi#struts23163,下载App即可,把里面的struts2-blank.war解压,把里面的jar全部导入即可。这是第一个问题。
第二个问题又来了,当时笔者不放心把lib下的jar包全部导入了,结果报错找不到包。这个原因是struts2的包一些自身就是高版本不支持低版本。那么我们究竟需要哪些jar包呢?下面笔者把截图发上。注意,经亲测这里的jar包是缺一不可的。

四:一个简单的测试用例

好了,烦心的配置之后我们就开始愉快的测试吧。
笔者测试了一个图书管理系统,下面先给出一个简单的用例让我们熟悉一下StrutsTestCase。之后我会在下一节给出一个比较棘手的问题。
这里我要演示的是一个通过作者姓名查询。为了突出核心问题,这里把bean里面的类就不贴了。要说明的是通过作者名做一些搜索工作,然后把该作者的全部书籍存到list,把list共享一下在后台取得并打印。下面的代码就模拟如何获取这个list。
QueryAction:
package com.amaker.action;

import org.apache.struts2.ServletActionContext;
import java.util.List;



public class QueryAction{
	
	private String Author;//the author's name
	private String Tip;
	public String execute()throws Exception{
		OperaDB db = new OperaDB();
		List<Book> list = db.Search(Author);
		if(list == null || list.isEmpty()){
			this.setTip("作者名错误或不存在,请重新输入!");
			return "error";
		}else{
			ServletActionContext.getRequest().getSession().setAttribute("List",list);
			ServletActionContext.getRequest().setAttribute("list",list);
			return "success";
		}
	}
	//getter and setter
	public String getTip() {
		return Tip;
	}

	public void setTip(String tip) {
		Tip = tip;
	}

	public void setAuthor(String author) {
		Author = author;
	}
	
}

QueryActionTest:

package com.amaker.action;

import java.util.List;
import org.apache.struts2.StrutsTestCase;
import com.opensymphony.xwork2.ActionProxy;

public class QueryActionTest extends StrutsTestCase{
  
	public void testSuccessQuery() {
		request.setParameter("Author","Margaret Mitchell");
		ActionProxy proxy = getActionProxy("/query");  
        try {
			proxy.execute();
			List results = (List)request.getAttribute("list");
			Book bk = (Book)results.get(0);
			assertEquals(bk.getTitle(),"Gone with the Wind");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
        
		
	}
	public void testFailQuery() {
		request.setParameter("Author"," ");
		ActionProxy proxy = getActionProxy("/query"); 
		QueryAction action = (QueryAction) proxy.getAction(); 
		try {
			proxy.execute();
	
			assertEquals(action.getTip(),"作者名错误或不存在,请重新输入!");
		} catch (Exception e) {
			
			e.printStackTrace();
		}
     

	
		
	}

}

可以看到我们测试成功了。

五:关于多级Action的测试方法

我们可以看到StrutsTestCase的特点了,这里是模拟单个网页的请求来测试某个Action,但是如果该网页还连接到其他网页或者还有其他二级响应该怎么办呢?因为要测试一个Action不能利用上部的结果,因为这种测试是静态的。那么怎么办呢?有两种思路:其一是再次人工模拟上次的结果在共享一下,二级响应再获取。当然这种方法当数据量比较大时不靠谱。第二种思路就是我们在测试二级Action时先模拟一级Action,下面以一个例子说明:
这个例子就是上个步骤显示出来书籍之后点击该书籍可以跳转到另一个页面显示该书籍的详细信息。
 index.java
package com.amaker.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

public class Index {
//the content to display
	private String index;
	private String ISBN;
	private String Title;
	private String Publisher;
	private String PublisherDate;
	private String Price;
	private String Name;
	private String Age;
	private String Country;
	private int AuthorID;
	

	public String execute() throws Exception{
		if(index==null)
			index=ServletActionContext.getRequest().getSession().getAttribute("index").toString();
		int num=Integer.parseInt(index);
		ServletActionContext.getRequest().getSession().setAttribute("index",index);
		@SuppressWarnings("unchecked")
		List<Book> list = (List<Book>) ServletActionContext.getRequest().getSession().getAttribute("List");
		Book bk = list.get(num);
		
		this.setAge(bk.getAge());
		this.setTitle(bk.getTitle());
		this.setAuthorID(bk.getAuthorID());
		this.setCountry(bk.getCountry());
		this.setISBN(bk.getISBN());
		this.setName(bk.getName());
		this.setPrice(bk.getPrice());
		this.setPublisherDate(bk.getPublisherDate());
		this.setPublisher(bk.getPublisher());
		return "success";
	}
//setter and getter
	public void setIndex(String index) {
		this.index = index;
	}
	public void setAuthorID(int authorID) {
		AuthorID = authorID;
	}
	public void setISBN(String iSBN) {
		ISBN = iSBN;
	}
	public void setTitle(String title) {
		Title = title;
	}
	public void setPublisher(String publisher) {
		Publisher = publisher;
	}
	public void setPublisherDate(String publisherDate) {
		PublisherDate = publisherDate;
	}
	public void setPrice(String price) {
		Price = price;
	}
	public void setName(String name) {
		Name = name;
	}
	public void setAge(String age) {
		Age = age;
	}
	public void setCountry(String country) {
		Country = country;
	}
	

}
IndexTest.java
package com.amaker.action;

import java.util.List;

import org.apache.struts2.StrutsTestCase;

import com.opensymphony.xwork2.ActionProxy;

public class IndexTest extends StrutsTestCase{

	public void testIndex() {
		ActionProxy proxy = getActionProxy("/getinfo");
		Index action = (Index)proxy.getAction();
		request.setParameter("Author","Hemingway");
		ActionProxy proxy1 = getActionProxy("/query"); 
		try {
			proxy1.execute();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		action.setIndex("0");
		
		try {
			proxy.execute();
			List results = (List)request.getAttribute("list");
			Book bk = (Book)results.get(0);
			assertEquals(bk.getAge(),"1899.7.21--1961.7.2");
			assertEquals(bk.getAuthorID(),7);
			assertEquals(bk.getCountry(),"America");
			assertEquals(bk.getISBN(),"9780099908500");
			assertEquals(bk.getName(),"Hemingway");
			assertEquals(bk.getPrice(),"54.30 yuan");
			assertEquals(bk.getPublisher(),"Arrow");
			assertEquals(bk.getPublisherDate(),"1994.8.18");
			assertEquals(bk.getTitle(),"The Sun Also Rises");
			
			
		} catch (Exception e) {
			
		}
		
		
	}

}

六:小结

我这里用到的例子全是自己的代码,希望能帮到大家。



猜你喜欢

转载自blog.csdn.net/simon_world/article/details/41291125
今日推荐