博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
junit4进行单元测试
阅读量:7154 次
发布时间:2019-06-29

本文共 3341 字,大约阅读时间需要 11 分钟。

一、前言

  提供服务的时候,为了保证服务的正确性,有时候需要编写测试类验证其正确性和可用性。以前的做法都是自己简单写一个控制层,然后在控制层里调用服务并测试,这样做虽然能够达到测试的目的,但是太不专业了。还是老老实实的编写测试类进行测试吧。

二、Junit4依赖

junit
junit
4.12
org.springframework
spring-test
4.2.5.RELEASE
test

  如果出现如下异常:

  

  则加入如下依赖。

javax.servlet
servlet-api
3.0.1
test

三、目录结构

  

 

四、测试类

  通过自动注入方式获取bean

import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.alibaba.fastjson.JSONObject;import com.yyjz.icop.usercenter.service.ISupplierService;import com.yyjz.icop.usercenter.vo.SupplierVO;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/config/applicationContext.xml",        "file:src/main/webapp/WEB-INF/config/applicationContext-jpa.xml",}) // 加载配置public class UserExtTest{    @Autowired    private ISupplierService supplierService;        @Test    public void addSupplier(){        SupplierVO vo = new SupplierVO();        vo.setSupplierId("1234567890");        vo.setUserName("hjzgg");        vo.setUserCode("hjzgg");        vo.setUserMobile("1567c637914");        JSONObject ans = supplierService.addSupplier(vo);        System.out.println(ans.toJSONString());    }}

   注:@ContextConfiguration中locations文件配置,如果文件放在了WEB-INF/config目录下,配置如上所示。如果配置文件放入src/main/resources目录下,则改成"calsspath:applicationContext.xml"和"calsspath:applicationContext-jpa.xml" 。

  同时,如果配置文件中引用了properties的文件,也要改一下路径。测试完成之后在改回去。

  

 

  通过上下文获取bean

import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import com.alibaba.fastjson.JSONObject;import com.yyjz.icop.usercenter.service.ISupplierService;import com.yyjz.icop.usercenter.service.impl.SupplierServiceImpl;import com.yyjz.icop.usercenter.vo.SupplierVO;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/config/applicationContext.xml",        "file:src/main/webapp/WEB-INF/config/applicationContext-jpa.xml",}) // 加载配置@WebAppConfigurationpublic class UserExtTest{    @Autowired    private ApplicationContext ac;        @Test    public void addSupplier(){        ISupplierService supplierService = ac.getBean(SupplierServiceImpl.class);        SupplierVO vo = new SupplierVO();        vo.setSupplierId("1234567890");        vo.setUserName("hjzgg");        vo.setUserCode("hjzgg");        vo.setUserMobile("15670637914");        JSONObject ans = supplierService.addSupplier(vo);        System.out.println(ans.toJSONString());    }}

  @WebAppConfiguration:测试环境使用,用来表示测试环境使用的ApplicationContext将是WebApplicationContext类型的;value指定web应用的根。默认值是:String value() default "src/main/webapp";

五、总结

  至此,测试类的简单使用完成。还需要更进一步学习测试类。

转载地址:http://plegl.baihongyu.com/

你可能感兴趣的文章
审计工具lynis介绍
查看>>
保卫智能手机安全,让 SSL 数字证书给你多一层安全保护
查看>>
MySQL 起停脚本
查看>>
大数据:一场改变未来的信息革命
查看>>
MAC OS X 安装、配置、启动 rabbitMQ
查看>>
解决webuploader 在chrome 浏览器反应迟钝问题
查看>>
让EditPlus支持javac,java命令[图解]
查看>>
Python初学者的一些技巧
查看>>
centos安装epel源
查看>>
想不到的异或操作。。
查看>>
理解UIApplication
查看>>
例子 /maven-service-factory-api
查看>>
iOS运行回路(RunLoop)总结
查看>>
链表crud
查看>>
GitHub Pages上写完简历后报404
查看>>
硬盘的读写原理
查看>>
eclipse svn时忽略target .project .classpath等目录文件
查看>>
告警系统主脚本、主配置文件、监控项脚本
查看>>
CSS层叠样式表之CSS解析机制的优先级及样式覆盖问题探讨
查看>>
angularjs关于controller之间如何通讯
查看>>