单元测试Struts2Spring项目的Action和Service

简介:
 最近,认真实践了单元测试Struts2、Spring等Java项目,今天特意写的是单元测试Struts2Spring项目的Action和Service。
  由于已经写过不少Web开发框架 单元测试的代码,加上上次从头搭建环境并发表了"单元测试Struts2的Action(包含源码) ",没有遇到太多问题。
  特别说明:本文是原创,搭建环境、写代码、运行,都是实践并且正确的。
  本文是靠谱的,而非简单的复制-粘贴。
   1.特别说明。
  http://blog.csdn.net/fansunion/article/details/12118043  单元测试Struts2的Action(包含源码)
  这篇 文章主要讲述的是如何 使用 JUnit等单元测试框架测试 Struts2这一个框架的Action。
  而本篇侧重Struts2和Spring这2个框架集成的情况。
  更多框架集成的单元测试Demo文章,请关注本博客后续 单元测试相关文章。
   2.新建工程,加入相关jar包。
  Struts相关jar包
  Spring相关jar包
  JUnit, spring-test-3.2.3.RELEASE.jar,struts2-junit-plugin-2.2.3.1.jar等测试相关jar包
  Tomcat的Servlet/JSP jar包
   3.新建Action。
package action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* 一个简单的Action。
* @author FansUnion
*
*/
@Controller
public class UserAction {
@Autowired
private UserService userService;
public String getName() {
userService.getName("FansUnion");
return "success";
}
}


4.新建Service。
package action;
import org.springframework.stereotype.Service;
@Service
public class UserService {
/**
* 简单的返回用户名
*/
public String getName(String name) {
// 这个地方实际上应该调用Dao持久层的代码,在此简化了。
// 本文只是单元测试Struts2Spring整合中,Struts的Action和Spring注入的Service。
// Struts2Spring+Hibernate/Mybatis将在后续篇章中介绍。
return name;
}
}
   5.单元测试Action。
package unittest;
import org.apache.struts2.StrutsSpringTestCase;
import org.junit.Test;
import action.UserAction;
import com.opensymphony.xwork2.ActionProxy;
/**
* 测试StrutsSpring集成的时候,需要继承StrutsSpringTestCase这个类,
* 而单独测试Struts的时候,继承StrutsTestCase。
*
* @author http://blog.csdn.net/fansunion/
*
*/
public class ActionUnitTest extends StrutsSpringTestCase {
// 重写父类方法,指定配置文件的名字
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.execute();
assertEquals("success", result);
}
}


4.新建Service。
package action;
import org.springframework.stereotype.Service;
@Service
public class UserService {
/**
* 简单的返回用户名
*/
public String getName(String name) {
// 这个地方实际上应该调用Dao持久层的代码,在此简化了。
// 本文只是单元测试Struts2Spring整合中,Struts的Action和Spring注入的Service。
// Struts2Spring+Hibernate/Mybatis将在后续篇章中介绍。
return name;
}
}
   5.单元测试Action。
package unittest;
import org.apache.struts2.StrutsSpringTestCase;
import org.junit.Test;
import action.UserAction;
import com.opensymphony.xwork2.ActionProxy;
/**
* 测试StrutsSpring集成的时候,需要继承StrutsSpringTestCase这个类,
* 而单独测试Struts的时候,继承StrutsTestCase。
*
* @author http://blog.csdn.net/fansunion/
*
*/
public class ActionUnitTest extends StrutsSpringTestCase {
// 重写父类方法,指定配置文件的名字
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.execute();
assertEquals("success", result);
}
}


4.新建Service。
package action;
import org.springframework.stereotype.Service;
@Service
public class UserService {
/**
* 简单的返回用户名
*/
public String getName(String name) {
// 这个地方实际上应该调用Dao持久层的代码,在此简化了。
// 本文只是单元测试Struts2Spring整合中,Struts的Action和Spring注入的Service。
// Struts2Spring+Hibernate/Mybatis将在后续篇章中介绍。
return name;
}
}
   5.单元测试Action。
package unittest;
import org.apache.struts2.StrutsSpringTestCase;
import org.junit.Test;
import action.UserAction;
import com.opensymphony.xwork2.ActionProxy;
/**
* 测试StrutsSpring集成的时候,需要继承StrutsSpringTestCase这个类,
* 而单独测试Struts的时候,继承StrutsTestCase。
*
* @author http://blog.csdn.net/fansunion/
*
*/
public class ActionUnitTest extends StrutsSpringTestCase {
// 重写父类方法,指定配置文件的名字
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.execute();
assertEquals("success", result);
}
}


  6.单元测试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;
/**
* 测试Service的基类
*
* @author http://blog.csdn.net/fansunion/
*
*/
public class JUnitTestBase {
public static XmlWebApplicationContext context = 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.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配置。
  (由于是从我的就项目复制类的,很多配置都可以去掉,由读者自己完成)
<?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配置。
<?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>
最新内容请见作者的GitHub页:http://qaseven.github.io/

相关文章
|
6月前
|
人工智能 测试技术 项目管理
测试不再碎片化:AI智能体平台「项目资料套件」功能上线!
在实际项目中,需求文档分散、整理费时、测试遗漏等问题常困扰测试工作。霍格沃兹推出AI智能体测试平台全新功能——项目资料套件,可将多个关联文档打包管理,并一键生成测试用例,提升测试完整性与效率。支持套件创建、文档关联、编辑删除及用例生成,适用于复杂项目、版本迭代等场景,助力实现智能化测试协作,让测试更高效、更专业。
|
9月前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
1015 0
|
5月前
|
安全 Java 测试技术
《深入理解Spring》单元测试——高质量代码的守护神
Spring测试框架提供全面的单元与集成测试支持,通过`@SpringBootTest`、`@WebMvcTest`等注解实现分层测试,结合Mockito、Testcontainers和Jacoco,保障代码质量,提升开发效率与系统稳定性。
|
6月前
|
测试技术 UED 开发者
性能测试报告-用于项目的性能验证、性能调优、发现性能缺陷等应用场景
性能测试报告用于评估系统性能、稳定性和安全性,涵盖测试环境、方法、指标分析及缺陷优化建议,是保障软件质量与用户体验的关键文档。
|
8月前
|
Java 测试技术 Spring
简单学Spring Boot | 博客项目的测试
本内容介绍了基于Spring Boot的博客项目测试实践,重点在于通过测试驱动开发(TDD)优化服务层代码,提升代码质量和功能可靠性。案例详细展示了如何为PostService类编写测试用例、运行测试并根据反馈优化功能代码,包括两次优化过程。通过TDD流程,确保每项功能经过严格验证,增强代码可维护性与系统稳定性。
327 0
|
9月前
|
安全 Java 测试技术
说一说 Spring Security 中的单元测试
我是小假 期待与你的下一次相遇 ~
178 1
|
8月前
|
人工智能 数据可视化 测试技术
UAT测试排程工具深度解析:让验收测试不再失控,项目稳稳上线
在系统交付节奏加快的背景下,“测试节奏混乱”已成为项目延期的主因之一。UAT测试排程工具应运而生,帮助团队结构化拆解任务、清晰分配责任、实时掌控进度,打通需求、测试、开发三方协作闭环,提升测试效率与质量。本文还盘点了2025年热门UAT工具,助力团队选型落地,告别靠表格和群聊推进测试的低效方式,实现有节奏、有章法的测试管理。
|
人工智能 自然语言处理 测试技术
Potpie.ai:比Copilot更狠!这个AI直接接管项目代码,自动Debug+测试+开发全搞定
Potpie.ai 是一个基于 AI 技术的开源平台,能够为代码库创建定制化的工程代理,自动化代码分析、测试和开发任务。
1342 19
Potpie.ai:比Copilot更狠!这个AI直接接管项目代码,自动Debug+测试+开发全搞定
|
缓存 Java 测试技术
【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
1813 3
【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
|
JSON 前端开发 API
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
815 5
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡