单元测试利器之Jtester

http://kiral.iteye.com/blog/900975

名词解释:

  • 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吧。

Java代码  收藏代码
    1. import mockit.NonStrict;  
    2.   
    3. import org.jtester.testng.JTester;  
    4. import org.jtester.unitils.jmockit.MockedBean;  
    5. import org.testng.annotations.Test;  
    6. import org.unitils.spring.annotation.SpringApplicationContext;  
    7. import org.unitils.spring.annotation.SpringBean;  
    8.   
    9. /** 
    10.  * Jtester测试例子,按照注释顺序学习 
    11.  *  
    12.  * @author tengfei.fangtf 
    13.  */  
    14. @SpringApplicationContext( { "applicationContext.xml" })  
    15. // 1.@SpringApplicationContext:加载Spring 配置文件,所有测试相关的bean都在这个容器中;  
    16. public class BusinessTestCase extends JTester// 2.JTester:要使用JTester  
    17. // 提供的功能,需要继承此基类;  
    18. {  
    19.   
    20.     @SpringBean("businessService")  
    21.     // 3.@SpringBean:从容器中取出指定id 的bean 并注入到测试类中  
    22.     private AppInternalService businessService;  
    23.   
    24.     @MockedBean  
    25.     @NonStrict  
    26.     // 4.@Mocked @MockedBean:mock 出一个对象,并将该对象与Spring 容器结合,实现Autowired;  
    27.     private OneHessianServiceClient hessianClient;  
    28.   
    29.     @Test(groups = { "FirstTestGroup" })  
    30.     // 5.@Test;TestNG 的注解;指明此方法为一个TestCase;  
    31.     public void testBusinessNormal() {  
    32.         new Expectations() {// 6.设置mock 对象的期望返回值  
    33.             {  
    34.                 hessianClient.hessianServiceInvorke(anyString);  
    35.                 result = "HH";// 那么执行这个方法,永远都返回HH  
    36.             }  
    37.         };  
    38.   
    39.         String returnResult = businessService  
    40.                 .bussinessService("Sample Business!");  
    41.         System.out.println("  ---> " + returnResult);// 输出HH  
    42.         want.string(returnResult).notNull();// want:JTester 框架提供的强大的断言;  
    43.     }  
    44.   
    45. }  
原文地址:https://www.cnblogs.com/dieyaxianju/p/5085067.html