Recently, I have carefully practiced Java projects such as unit test Struts2 and Spring. Today I specifically wrote the actions and services of the Unit Test Struts2Spring project.
I have already written a lot of code for unit testing in the Web development framework, and I wrote the "unit test Struts2 Action (including the source code)" from the ground up last time.
Note: This article is original. It is both practical and correct to set up the environment, write code, and run.
This article is reliable, not simply copy-paste.
1. special instructions.
Action of http://blog.csdn.net/fansunion/article/details/12118043 unit test Struts2 (including source code)
This article mainly describes how to use the unit test framework such as JUnit to test the Action of the Struts2 framework.
This article focuses on the integration of Struts2 and Spring frameworks.
For more unit test Demo articles on framework integration, please follow the subsequent unit test articles in this blog.
2.Create a project and add relevant jar packages.
Struts-related jar packages
Jar packages related to Spring
JUnit, spring-test-3.2.3.RELEASE.jar, struts2-junit-plugin-2.2.3.1.jar and other test-related jar packages
Tomcat Servlet/JSP jar package
3. Create an Action.
Package action; import org. springframework. beans. factory. annotation. Autowired; import org. springframework. stereotype. Controller;/*** a simple Action. * @ Author FansUnion **/@ Controllerpublic class UserAction {@ Autowired private UserService userService; public String getName () {userService. getName ("FansUnion"); return "success ";}}
4. Create a Service.
Package action; import org. springframework. stereotype. service; @ Servicepublic class UserService {/*** simple return username */public String getName (String name) {// code of the Dao persistence layer should be called in this field, this is simplified. // This article is only a unit test of Struts Action and Spring injection Service in Struts2Spring integration. // Struts2Spring + Hibernate/Mybatis will be introduced in subsequent chapters. Return name ;}}
5. unit test Action.
Package unittest; import org. apache. struts2.StrutsSpringTestCase; import org. junit. test; import action. userAction; import com. opensymphony. xwork2.ActionProxy;/*** when testing StrutsSpring integration, you must inherit the StrutsSpringTestCase class. * When testing Struts separately, you must inherit StrutsTestCase. ** @ Author http://blog.csdn.net/fansunion/ **/public class ActionUnitTest extends StrutsSpringTestCase {// override the parent class method, specifying the name of the configuration file protected String [] getContextLocations () {return new String [] {"struts. xml "," applicationContext-spring.xml "};}@ Test public void testExecute () throws Exception {ActionProxy proxy = getActionProxy ("/unitTest "); UserAction test = (UserAction) proxy. getAction (); assertNotNull (test); String result = proxy.exe cute (); assertEquals ("success", result );}}
6. Unit Test Service.
Package unittest; import org. junit. afterClass; import org. junit. beforeClass; import org. springframework. mock. web. mockServletContext; import org. springframework. web. context. webApplicationContext; import org. springframework. web. context. support. xmlWebApplicationContext;/*** Test Service base class *** @ author http://blog.csdn.net/fansunion/ ***/public class JUnitTestBase {public static XmlWebApplicationContext co Ntext = null; public static String [] CONFIG_FILES = {"file: src/applicationContext -*. xml "}; public JUnitTestBase () {System. out. println ("JUnitTestBase") ;}@ BeforeClass public static void setUp () {System. out. println ("Test start... "); Context = new XmlWebApplicationContext (); context. setConfigLocations (CONFIG_FILES); MockServletContext msc = new MockServletContext (); context. setServletContext (msc); context. refresh (); msc. setAttribute (WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context) ;}@ AfterClass public static void tearUp () {System. out. println ("Test end! ") ;}} Package unittest; import static org. junit. assert. assertEquals; import org. junit. test; import action. userService; public class UserServiceTest extends JUnitTestBase {private UserService userService; public UserServiceTest () {userService = context. getBean (UserService. class);} @ Test public void test () {String name = "http://FansUnion.cn"; String myName = userService. getName (name); assertEquals (name, myName );}}
7. Configure Struts2.
<struts> <!– Development Mode –> <constant name="struts.devMode" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.objectFactory.spring.autoWire" value="name" /> <package name="manager" namespace="/" extends="struts-default"> <action name="unitTest" class="userAction" method="getName"> <result name="success">unitTest.jsp </result> </action> </package></struts>
8. Spring configuration.
(Because I copied the class from my project, many configurations can be removed and completed by the readers themselves)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/flex classpath:/spring-flex-1.0.xsd "> <context:annotation-config /> <context:component-scan base-package="*" /> <aop:aspectj-autoproxy /></beans>
9. web. xml configuration.
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!– Spring Context –> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext*.xml</param-value> </context-param> <!– Spring Context Listener –> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
10. Download the source code.
Download source code CSDN:(No points)
If you have any questions, you can leave a message or join a group or QQ. I took the time to reply.
For the original article, see: 2550