名词解释:
什么是Jtester?
jTester是一个基于java的单元测试框架。开源地址:http://code.google.com/p/java-tester/。
为什么要用Jtester?
JTester是站在众多巨人肩膀上的单元测试框架,集成了Junit4.5,dbunit2.4.3,unitils2.2,JMOCK2.5和TestNg5.1这些优秀的开源框架,并在这些框架上做了扩展,使得单元测试更加方便和强大。
Jtester带给了我们什么?
1、在unitils的基础,集成了jmock功能。
2、在hamcrest断言的基础上,实现了fluent interface断言。
3、改造了jmock expectation参数断言为fluent interface形式
4、录制对象:提供了将普通的pojo对象序列化到文件,然后再从文件中反序列化回来的功能,用于在对象复杂的情况下,直接录制接口(远程接口)调用返回的对象,以供下次测试或调试使用。
5、数据测试:使用wiki代替xml来准备测试数据。比dbunit更快准备数据。
6、实现了更加丰富的断言。比junit的断言多。
7、提供了hibernate annotation环境下,直接使用内存数据库进行db测试。
8、提供了hibernate annotation环境下,Open Test in Session的实现。
以上8大特性来自于官方,我稍加了点说明和整理。
七步进入Jtester世界。下面让我们花一个泡面的时间来学习下Jtester吧。
import mockit.NonStrict;
import org.jtester.testng.JTester;
import org.jtester.unitils.jmockit.MockedBean;
import org.testng.annotations.Test;
import org.unitils.spring.annotation.SpringApplicationContext;
import org.unitils.spring.annotation.SpringBean;
/**
* Jtester测试例子,按照注释顺序学习
*
* @author tengfei.fangtf
*/
@SpringApplicationContext( { "applicationContext.xml" })
// 1.@SpringApplicationContext:加载Spring 配置文件,所有测试相关的bean都在这个容器中;
public class BusinessTestCase extends JTester// 2.JTester:要使用JTester
// 提供的功能,需要继承此基类;
{
@SpringBean("businessService")
// 3.@SpringBean:从容器中取出指定id 的bean 并注入到测试类中
private AppInternalService businessService;
@MockedBean
@NonStrict
// 4.@Mocked @MockedBean:mock 出一个对象,并将该对象与Spring 容器结合,实现Autowired;
private OneHessianServiceClient hessianClient;
@Test(groups = { "FirstTestGroup" })
// 5.@Test;TestNG 的注解;指明此方法为一个TestCase;
public void testBusinessNormal() {
new Expectations() {// 6.设置mock 对象的期望返回值
{
hessianClient.hessianServiceInvorke(anyString);
result = "HH";// 那么执行这个方法,永远都返回HH
}
};
String returnResult = businessService
.bussinessService("Sample Business!");
System.out.println("\n ---> " + returnResult);// 输出HH
want.string(returnResult).notNull();// want:JTester 框架提供的强大的断言;
}
}
分享到:
相关推荐
详细的介绍了junit单元测试的过程,JUnit 是 Java 社区中知名度最高的单元测试工具,本文中所介绍的是最新的JUnit 4单元测试,JUnit 4 使用 Java 5 中的注解(annotation)使测试变得更加简单
Moq .NET单元测试利器,参考对于资料熟悉如何使用MOQ进行单元测试。
单元测试利器JUnit4
这个主要是初学者适用的,简单适用,赶快检查你的代码的高效性吧!
单元测试利器 JUnit 4 完整实例图解 做测试的必看的文档 希望对您有帮助
单元测试利器JUnit4
最新 Nunit .NET开发人员单元测试利器 最新版2.60
已经编译好了debug版本和release版本,而且头文件也完全包含了 编译好的release版文件包含:cppunit.lib,cppunit_dll.lib,cppunit_dll.exp,cppunit_dll.dll,TestRunner.lib,TestRunner.exp,TestRunner.dll,...
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典
[浪曦][原创]单元测试利器-Unit,经典中的经典