In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use Junit to test Action in Struts2. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Add JUnit Plugin and its dependent Spring package to the original Struts2 project:
Struts2-junit-plugin-2.1.8.jar org.springframework.core-3.0.3.RELEASE.jar org.springframework.test-3.0.3.RELEASE.jar
2. Create a new Action. I use the Annotation method here to achieve zero configuration. TestAction.java
@ Result (location = "/ index.jsp") public class TestAction extends ActionSupport {private String param; public String execute () {return SUCCESS;} public String getParam () {return param;} public void setParam (String param) {this.param = param;}}
3. Add the JUnit package to the project (this step is omitted, so don't read this article if you can't handle this one!) and create a TestCase,TestActionTest.java.
Import java.robinleex.action.TestAction; import org.apache.struts2.StrutsTestCase; import org.junit.Test; import com.opensymphony.xwork2.ActionProxy; public class TestActionTest extends StrutsTestCase {@ Test public void testExecute () throws Exception {ActionProxy proxy = null; TestAction test = null; request.setParameter ("param", "test..."); proxy = getActionProxy ("/ test.action") Test = (TestAction) proxy.getAction (); String result = proxy.execute (); assertEquals ("success", result); assertEquals ("test...", test.getParam ());}}
This example test passes in a parameter in Action and fetches the parameter value. JUnit Plugin simulates the Servlet container and simulates HttPServletRequest,HttpServletResponse,ServletContext,PageContext...
Thank you for reading! This is the end of this article on "how to use Junit to test Action in Struts2". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.