FAQs about StrutsTestCase configuration and how to handle multi-level actions
StrutsTestCase configuration FAQ and multi-level Action Processing Method 1: the reason for writing this article StrusTestCase is a common test method, and there are many online tutorials. But why should I write this article? First, there are two or three online tutorial versions. Basically, all of them copy each other and the tutorials are outdated. Second, many people on the Internet have not provided available solutions to many problems. The following describes some of your problems and solutions. Ii. Basic principles of StrutsTestCase. StrutsTestCase is an extension class of Junit and is mainly used to test the MVC project. The following tutorial is used for a purely Struts2 project. StrutsTestCase is a Junit-based testing framework used to test Struts behavior. If you use Struts, you will notice that it can provide you with an easy and effective way to test the Struts behavior class of your application. A clear point is to test a specific Action, simulate the input information from the web page, and then run the test class excute, the result is compared with the expected value to test whether your project has any problems. For more detailed principles, you can find some information on Baidu. The focus of this article is not here. III. The StrutsTestCase FAQ is actually the purpose of writing this article. Here are some of the pitfalls I have encountered. The first problem is that I introduced the struts2-Junit-Plugin and some Spring jar package, found that the various types can not find, that is, did not find the jar package, but I clearly introduced the jar package? After a while, I finally found the problem. It turns out that the core jar package of struts2 in my project has a problem. The struts2 jar package in that version finds the StrutsTestCase class, but there is no function in this class. However, eclipse reminds us that the StrutsTestCase class provided in the struts2 core jar is used by default. I am also wondering why I provided this interface but did not provide the corresponding test function? Okay, now we have to change the core package of struts2. The author uses the core package of the latest version. This version does not have this problem: Kernel. This is the first problem. The second problem came again. At that time, I was not at ease importing all the jar packages under lib, and the result showed that the package could not be found. This is because some of the struts2 packages are of the higher version and do not support the lower version. So what jar packages do we need? Next I will send it to you.Note that the jar package here is indispensable.4. A simple test case is ready. Let's start a pleasant test after worrying about the configuration. I tested a library management system. Next I will give a simple example to familiarize us with StrutsTestCase. Then I will give you a difficult question in the next section. Here I want to demonstrate a query by the author's name. To highlight the core issue, the classes in the bean will not be pasted here. It should be noted that some search work is done by the author name, and then all the books of the author are saved to the list, share the list and obtain and print it in the background. The following code simulates how to obtain the list.QueryAction:
Package com. amaker. action; import org. apache. struts2.ServletActionContext; import java. util. list; public class QueryAction {private String Author; // the author's nameprivate String Tip; public String execute () throws Exception {OperaDB db = new OperaDB (); List
List = db. Search (Author); if (list = null | list. isEmpty () {this. setTip ("the Author name is incorrect or does not exist. Please enter it again! "); Return" error ";} else {ServletActionContext. getRequest (). getSession (). setAttribute ("List", list); ServletActionContext. getRequest (). setAttribute ("list", list); return "success" ;}}// getter and setterpublic 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 using proxy.exe cute (); 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 using proxy.exe cute (); assertEquals (action. getTip (), "The author name is incorrect or does not exist. Please enter it again! ");} Catch (Exception e) {e. printStackTrace ();}}}
We can see that the test is successful.
V. Test Methods for multi-level actionsWe can see the features of StrutsTestCase. Here we simulate a single webpage request to test an Action. But what if the webpage is still connected to another webpage or has other level-2 responses? Because the above results cannot be used to test an Action, because the test is static. So what should we do? There are two ways: one is to manually simulate the previous result again and share it, and then obtain the second-level response. Of course, this method is unreliable when the data volume is large. The second approach is to simulate a level-1 Action when testing a level-2 Action. The following is an example: in this example, the detailed information of the book is displayed on another page after the book is displayed in the previous step. Index. java
package com.amaker.action;import java.util.List;import org.apache.struts2.ServletActionContext;public class Index {//the content to displayprivate 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
list = (List
) 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 getterpublic 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) {}}}
6. Summary The examples I use here are all my own code, hoping to help you.