`
nanjingjiangbiao_T
  • 浏览: 2585171 次
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

单元测试利器之Jtester

 
阅读更多

名词解释:

  • Junit:众所周知的单元测试。 官方网址:http://junit.sourceforge.net/。Junit从4.0开始提供基于注解的配置方式。
  • Dbunit: 一个针对数据库测试的框架,通过Excel准备数据并插入数据库。官方地址:http://www.dbunit.org/
  • TestNG:从字面上可以理解为下一代单元测试,和Junit的功能有一部分重叠和区别。TestNG也有Eclipse插件,官方地址http://testng.org/doc/index.html
  • Unitiles: 让单元测试变得更加容易和可维护,Unitils构建在DBUnit之上并与JUnit和TestNG相结合。官方地址:http://www.unitils.org
  • Jmock:JMock是一个JAVA开发包,它支持Mock(模拟)对象机制的TDD(测试驱动开发),官方地址:http://www.jmock.org/


什么是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 框架提供的强大的断言;
	}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics