qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

在SSH中增加JUNIT4单元测试

  首先,需要将UNITILS相关的jar包导入到系统的依赖jar包文件lib中,相关的jar文件如下:
dbunit-2.4.8.jar
hamcrest-all-1.3.0RC2.jar
hamcrest-library-1.1.jar
htmlunit-2.8.jar
htmlunit-core-js-2.8.jar
httpclient-4.0.2.jar
httpcore-4.0.1.jar
httpmime-4.0.1.jar
junit-4.8.2.jar
mockito-all-1.8.5.jar
nekohtml-1.9.14.jar
operadriver-v0.6.jar
selaid-1.0.1.jar
selenium-java-2.4.0.jar
spring-ws-test-0.22.jar
testng-5.14.4.jar
unitils-core-3.1.jar
unitils-database-3.1.jar
unitils-dbmaintainer-3.1.jar
unitils-dbunit-3.1.jar
unitils-mock-3.1.jar
unitils-orm-3.1.jar
unitils-spring-3.1.jar
unitils-testng-3.1.jar
xmlunit-1.2.jar
xmlunit-1.3.jar
  此外,除了常见的jar包之外,还有一个commons-lang包也是需要的,具体报错的时候,在网上找一下就ok了.
  然后,使用Eclipse的new功能,创建一个基于Junit4的test case,需要注意的事情是,需要让该test case能够得到spring 的SpringApplicationContext对象,得到了Spring的SpringApplicationContext对象之后,后面的测试用例就比较简单了.在获取SpringApplicationContext的时候,一定要注意注解里面的文件位置的获取.我们可以多看一下编译之后的文件,不要仅仅依据eclipse的文件的位置来判断相对位置.下面是我的Test Case的父类,以及子测试类:
package com.ziwen.common;
import org.springframework.context.ApplicationContext;
import org.unitils.UnitilsJUnit4;
import org.unitils.spring.annotation.SpringApplicationContext;
public class CommonTest extends UnitilsJUnit4{
@SpringApplicationContext({
"springConfig/applicationContext.xml",
"springConfig/bizContext.xml",
"springConfig/daoContext.xml"})
private ApplicationContext applicationContext;
public ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
子类:
package com.ziwen.biz;
import static org.junit.Assert.assertNotNull;
public class TestDemoBiz extends CommonTest{
@SpringBean("demoBiz")
private DemoBiz demobiz;
@Test
public void testDoSth() {
List<Demo> list=demobiz.getDemoList(new Page(10));
assertTrue(list.size()>1);
}
@Test
public void testGetDemoList() {
String str=demobiz.doSth();
assertNotNull(str);
}
}


 测试的结果:

posted on 2013-09-25 11:33 顺其自然EVO 阅读(322) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2013年9月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜