这里总结几种有效的配置单元测试方式:
1、直接使用junit4组件,进行简单单元测试,在setup()中初始化各种测试环境如下:
@override
protected void setup() throws exception {
super.setup();
string[] paths = { "classpath:applicationcontext.xml" };
applicationcontext ctx = new classpathxmlapplicationcontext(paths);
springcontextlistener.setapplicationcontext(ctx);
// ############模拟servlet容器启动,手动更改配置文件路径
constant.current_project_path = "src/";
// ############直接容器中获取bean
usermanager = (usermanager) ctx.getbean("usermanager");
userservice = (userservice) ctx.getbean("userservice");
}
2、使用sprint-test.jar框架,支持事务可注解方式启动的单元测试:
@runwith(springjunit4classrunner.class)
@contextconfiguration(locations = "classpath:applicationcontext.xml")
@transactionconfiguration(transactionmanager="transactionmanager",defaultrollback=false) //true将会回滚所有操作,默认没有该注解为回滚
public class jbpmprocesstest2 extends abstracttransactionaljunit4springcontexttests{
以上都只能暂时支持简单功能的单元测试。但是要测试并发和压力等。需要加入一个常用的框架,支持在单元测试中注入多线程和并发。
具体参见:http://blog.csdn.net/zhangyaoming2004/article/details/7619489 下面附有jar包下载 http://yunpan.cn/qutvay66vnu4t
我们具体使用的功能不是很多,这些并发测试可以嵌入到上面两种单元测试方式中:如下:
/**
*
*/
package com.dtsz.model.service.test;
import net.sourceforge.groboutils.junit.v1.testrunnable;
import com.dtsz.model.entity.report.common.invokesource;
import com.dtsz.model.service.excelreportlogicservice;
import com.dtsz.model.service.tasklogicservice;
import com.dtsz.model.util.excelreportutil;
import com.dtsz.view.vo.authenticator.uservo;
import com.dtsz.view.vo.webservice.excelreportresult;
* 模拟插件端数据上报的并发操作测试
* @author xiaoli
public class createmulltitaskreportgrobothread extends testrunnable {
tasklogicservice tasklogicservice;
excelreportlogicservice excelreportlogicservice;
private string reportmap;
private uservo user;
private invokesource invokesource;
private string bbq;
public createmulltitaskreportgrobothread(string reportmap, uservo user, invokesource invokesource,string bbq) {
super();
this.reportmap = reportmap;
this.user = user;
this.invokesource = invokesource;
this.bbq = bbq;
init();
public void init() {
tasklogicservice = (tasklogicservice)excelreportutil.getbean("tasklogicservice");
excelreportlogicservice = (excelreportlogicservice)excelreportutil.getbean("excelreportlogicservice");
/*
* (non-javadoc)
* @see net.sourceforge.groboutils.junit.v1.testrunnable#runtest()
public void runtest() throws throwable {
// todo auto-generated method stub
// ########################模拟当前任务该报表期任务上报
//保存数据
excelreportresult result = tasklogicservice.reportdata(reportmap, user, invokesource);
excelreportlogicservice.savedata(result);
system.out.println("报表期为:"+this.bbq+"已上报");
单元测试并发:
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import junit.framework.testcase;
import net.sourceforge.groboutils.junit.v1.multithreadedtestrunner;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
import com.dtsz.model.entity.authenticator.user;
import com.dtsz.model.service.authenticator.usermanager;
import com.dtsz.model.service.authenticator.userservice;
import com.dtsz.model.service.exception.baseappexception;
import com.dtsz.model.util.beanutil2;
import com.dtsz.model.util.constant;
import com.dtsz.model.util.encryption;
import com.dtsz.view.util.springcontextlistener;
* 模拟数据上报,这里的初始化数据根据插件端传入数据组合来的,当前只有根据报表期生成初始化数据
public class createmultitaskreporttest3 extends testcase {
private usermanager usermanager;
private userservice userservice;
protected void teardown() throws exception {
super.teardown();
@test
public void test(){
// ###########自动生成报表期串集合
list<string> bbqs = new arraylist<string>();
unittestutils.imitatecreatemultibbq(bbqs, 2, "2048", "5");
// 创建线程
testrunnable[] tr = new testrunnable[bbqs.size()];
int processcount = 0;
// ##########对这些数据进行填报
for (string bbq : bbqs) {
// 模拟上报数据
string reportmap = "{taskid:\"402880ee425a92f501425aa75ad50002\",bbq_:\""
+ bbq
+ "\""
+ ",bbhid:\"402855b942099b1b014209b1177e03f5\",approvalflag:2,reportflag:false,auditflag:false,sheetmap:"
+ "[{id:\"402880ee425a92f501425aa75ad60004\",sheettype:\"basic\",values:{}}]}";
// 模拟上报的user
uservo user = null;
try {
user = checkuser("xl1", "1");
} catch (baseappexception e) {
e.printstacktrace();
} // 模拟插件端checkuser
string sessioninfo = "{institutionname:\"南昌分行\",institutioncode:\"a01nc\",username:\"肖力1"
+ "1\",usercode:\"xl1\",rolecode:\"null\",bbhid:\"402855b942099b1b014209b1177e03f5\",bbhcode:\"b01b\",btype:\"basic\",taskid:\"402880ee425a92f501425a"
+ "a75ad50002\",bbhname:\"基层财务b\",bbq:\"" + bbq
+ "\",frequency:\"month\",sessiondimension:{}}";
userservice.updatesession(sessioninfo, user);
} catch (sqlexception e) {
} // 模拟插件端updatesession
// 初始化上报方式
invokesource invokesource = invokesource.wscall;
createmulltitaskreportgrobothread reportthread = new createmulltitaskreportgrobothread(reportmap,user, invokesource,bbq);
tr[processcount] = reportthread;
processcount++;
system.out.println("#######################################计时开始########################");
long starttime = system.currenttimemillis();
// 生成测试线程运行器
multithreadedtestrunner mttr = new multithreadedtestrunner(tr);
// 运行测试线程
mttr.runtestrunnables();
} catch (throwable e) {
system.out.println("#######################一共耗时:"
+ string.valueof((system.currenttimemillis() - starttime) / 1000) + "秒");
* xl1 1
* */
public uservo checkuser(string userid,
string password) throws baseappexception {
map<string, string> parammap = new hashmap<string, string>();
parammap.put("username", userid);
parammap.put("password", password);
list<user> users = usermanager.findbycodecache(userid);
list<user> needusers = new arraylist<user>();
if (users != null && users.size() > 0) {
for (user user : users) {
if (user.getstate() && user.getcode().equals(userid)
&& user.getpassword().equals(encryption.encryption(password))) {
needusers.add(user);
// 通过验证
if (needusers != null && needusers.size() > 0) {
user user = needusers.get(0);
// 获取用户权限,并存放于session中
// uservo uservo = usermanager.getuserpermission(user.getid());
string ipaddress = "0.0.0.0";
uservo uservo = new uservo();
beanutil2.copyproperties(user, uservo);
uservo.setipaddress(ipaddress);
return uservo; // 通过验证
} else {
return null;
真实环境并发和压力测试不仅仅是在多个线程同时启动在测,还可能是增量式压力测试。这些还是要封装好和处理好。
最新内容请见作者的github页:http://qaseven.github.io/