单元测试代码:SpringTest+JUnit
/**
* JUnit单元测试父类,配置了Spring的基础环境。 <br/>
* 可以作为Controller、Service、Dao单元测试的父类。
*
* @author leiwen@fansunion.cn
*/
public class JUnitTestBase { public static XmlWebApplicationContext context = null; private static HandlerMapping handlerMapping;
private static HandlerAdapter handlerAdapter; // /public static String[] CONFIG_FILES = { "classpath:spring-*.xml" }; // public static String[] configs = { "file:src/main/resources/spring-*.xml"
// };
public static String[] CONFIG_FILES = { "file:src/main/resources/spring-*.xml" }; /**
* 读取spring配置文件,初始化上下文。
*/
@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); handlerMapping = (HandlerMapping) context
.getBean(DefaultAnnotationHandlerMapping.class);
handlerAdapter = (HandlerAdapter) context.getBean(context
.getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]); } // 执行request对象请求的action
public ModelAndView excuteAction(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 这里需要声明request的实际类型,否则会报错
request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true); HandlerExecutionChain chain = handlerMapping.getHandler(request);
Object handler = chain.getHandler();
final ModelAndView model = handlerAdapter.handle(request, response,
handler);
return model;
} @AfterClass
public static void tearUp() {
System.out.println("Test end!");
} } //测试Controller的2种方法 /**
* CampaignGroupController单元测试。
*
* @author leiwen@fansunion.cn
*/
public class FansUnionControllerTestextends JUnitTestBase { @Test
public void prevAddCampaignGroup() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
String requestURI = "/campaignGroup/prevAdd";
request.setRequestURI(requestURI);
request.setMethod("POST");
final ModelAndView mav = this.excuteAction(request, response);
Assert.assertEquals("createCampaignGroup", mav.getViewName()); } @Test
public void prevAddCampaignGroup2() throws Exception {
CampaignGroupController userController = context.getBean(CampaignGroupController.class);
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(userController)
.build();
ResultActions perform = mockMvc.perform(MockMvcRequestBuilders.post("/campaignGroup/prevAdd"));
ResultActions andExpect = perform
.andExpect(MockMvcResultMatchers.status().is(200));
andExpect
.andExpect(
MockMvcResultMatchers.view()
.name("createCampaignGroup"));
} } ----需要配置2个bean,测试Controller需要用到 <bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
</bean> 测试Service(Dao类似) /**
* 这种方式 的单元测试,不需要重复指定 资源文件,减少重复。 麻烦的地方在于,需要手动获取bean。
*
* @author leiwen@fansunion.cn
*/
public class FansUnionServiceTest extends JUnitTestBase { private static CampaignGroupService campaignGroupService = context
.getBean(CampaignGroupService.class); // 测试add,remove,update方法的时候,已经测试了find?
@Test
public void addCampaignGroup() {
CampaignGroup cg = buildOneGroup(); campaignGroupService.add(cg); CampaignGroup dbCampaignGroup = campaignGroupService.find(cg.getId());
Assert.assertEquals(cg, dbCampaignGroup);
campaignGroupService.delete(cg.getId());
} 基于注解的测试 /**
*
* 基于注解的单元测试。
*
* @author leiwen@fansunion.cn
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-mvc-context.xml",
"classpath:spring-common-context.xml" })
public class FansUnionServiceTestWithAnnotation { @Autowired
private CampaignGroupService campaignGroupService; /**
* 根据campaignId获取campaignGroup的name集合<br>
* 测试场景:<br>
*
*/
@Test
public void testListCampaignGroupNameByCampaignId(){
System.out.println("**********");
System.out.println(campaignGroupService.listCampaignGroupNameByCampaignId(1005899));
} }
参考资料:http://lohasle.iteye.com/blog/1617929
http://jiuyuehe.iteye.com/blog/1882424
单元测试代码:SpringTest+JUnit的更多相关文章
- JUnit编写单元测试代码注意点小结
用eclipse编写单元测试的时候,可以直接选中某个类,然后右键new新疆一个junit case,界面如下图1所示: 图1:新建test case 选 择图1中的JUnit Test Case,然后 ...
- 在Android Studio进行“简单配置”单元测试(Android Junit)
起因 在Android studio 刚出.本人就想弄单元测试,可惜当时Android studio不知道抽什么风(准确来说,应该是我不会弄而已).无法执行到相应的代码.后来今天突然自己又抽风.又想去 ...
- 单元测试系列:JUnit单元测试规范
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6762032.html Junit测试代 ...
- 单元测试简介和Junit的使用介绍
单元测试简介和Junit的使用介绍 Junit是Java开发中用来支持单元测试的一个软件,这里对它的基本情况.使用方法等做简单的介绍. 提纲 1.软件测试 2.单元测试概述 3.单元测试的具体做法 4 ...
- 真正意义上的spring环境中的单元测试方案spring-test与mokito完美结合
真正意义上的spring环境中的单元测试方案spring-test与mokito完美结合 博客分类: java 测试 单元测试SpringCC++C# 一.要解决的问题: spring环境中 ...
- python的单元测试代码编写流程
单元测试: 单元测试是对单独的代码块分别进行测试, 以确保它们的正确性, 单元测试主要还是由开发人员来做, 其余的集成测试和系统测试由专业的测试人员来做. python的单元测试代码编写主要记住以下几 ...
- 编写高质量代码改善C#程序的157个建议——建议155:随生产代码一起提交单元测试代码
建议155:随生产代码一起提交单元测试代码 首先提出一个问题:我们害怕修改代码吗?是否曾经无数次面对乱糟糟的代码,下决心进行重构,然后在一个月后的某个周一,却收到来自测试版的报告:新的版本,没有之前的 ...
- Spring整合JUnit框架进行单元测试代码使用详解
一.Spring提供的JUnit框架扩展: 1. AbstractSpringContextTests:spring中使用spring上下文测试的Junit扩展类,我们一般不会使用这个类来进行单元 ...
- JUnit单元测试代码
package com.storage.test; import org.junit.Before; import org.junit.Test; import org.springframework ...
随机推荐
- Android给定坐标计算距离
给定两点的经纬度.计算两点之间的距离.这里要注意经纬度一定要依照顺序填写 1. 利用android中的工具获得,单位是米 float[] results=new float[1]; Location. ...
- 阿里云 Docker-registry 搭建
阿里云 仓库地址: https://cr.console.aliyun.com/cn-hangzhou/instances/images
- 2018GDOI记
今年居然是主场.就没有游了. 向死而生.发现最近生活就是印证了我blog的那句话:就算是修罗,也会被生活玩弄于股掌间 想了很久,还是决定要继续写,然后公诸于众. ------------------- ...
- bzoj2595 [Wc2008]游览计划——斯坦纳树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2595 今天刚学了斯坦纳树,还不太会,写一道题练习一下: 参考了博客:http://www.c ...
- HTML中的文本框textarea标签
转自:https://www.jb51.net/web/183411.html <textarea></textarea>用来创建一个可以输入多行的文本框,此标志对用于< ...
- Rocky(模拟)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2718 题意:如果没有障碍就按原方向直走,否则就 ...
- C99新增内容之变长数组(VLA)
我们在使用多维数组是有一点,任何情况下只能省略第一维的长度.比如在函数中要传一个数组时,数组的行可以在函数调用时传递,当属数组的列却只能在能被预置在函数内部.看下面一个例子: #define COLS ...
- 遍历WPF DataGrid单元格
using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; ...
- pgsql 远程机器无法连接数据库报错处理方法
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本地localhost访问正常,在相同网段的远程机器访问报如下错误 “server closed the connection unexpe ...
- 智能识别快递地址api接口实现(PHP示例)
电商.ERP等行业发货时,批量录入图片上的收件人地址是个难题:智能识别收件人API是近乎完美的解决方案,通过识别图片,解析出图片中收件人的姓名.电话.详细地址(省.市.区/县.详细地址).将此接口集成 ...