Junit4学习总结

fmms 12年前

Junit4在工作中一直只关注简单的单元测试应用,并没有进行系统一点的了解。这两天在修改测试用例时,对它进行了一些了解。现把自己的想法记录下来,以便加深记忆和日后查看。

Junit4.x后的jar包中,存在两类:org.junit.*和junit.framework.*

其中,

junit.framework.*是Junit4之前的包,需要继承TestCase类实现单元测试类的编写,且每个测试方法都要求以testxxx规则命名,Junit框架才能运行该测试方法。

org.junit.*是Junit4之后的包,不再需要继承任何类,只需要使用注释(@Test)即可实现测试方法的自动调用,测试方法命名也自定义,不需再遵守testxxx的命名规则。

Junit的各测试方法的执行顺序不是按代码中的顺序执行,顺序无法指定,不同的运行平台执行顺序也会有差异。

以下是针对以上两种Junit的使用示例:

1.Junit4.x版本以前的测试方法:

package com.self;    import junit.framework.TestCase;    public class TestJunitCase extends TestCase {      public TestJunitCase(String methodName) {    super(methodName);   }      /**    * 测试方法1    */   public void testFirst() {    System.out.println("first Test Case...");   }     /**    * 测试方法2    */   public void testSecond() {    System.out.println("second Test Case...");   }     /**    * 测试方法3    */   public void testThird() {    System.out.println("third Test Case...");   }     }

测试结果如下:

first Test Case...  second Test Case...  third Test Case...

提示:若需要对测试用例的执行顺序进行指定,可通过以下方法进行调整。(建议各测试用例保持相对独立,尽量解耦,减少依赖及不必要的影响)

package com.self;    import junit.framework.Test;  import junit.framework.TestCase;  import junit.framework.TestSuite;      public class TestJunitCase extends TestCase {      public TestJunitCase(String methodName) {    super(methodName);   }      /**    * 可通过该方法调整各测试方法的执行顺序    * @return    */   public static Test suite() {    TestSuite suite = new TestSuite();    suite.addTest(new TestJunitCase("testSecond"));    suite.addTest(new TestJunitCase("testThird"));    suite.addTest(new TestJunitCase("testFirst"));    return suite;   }     /**    * 测试方法1    */   public void testFirst() {    System.out.println("first Test Case...");   }     /**    * 测试方法2    */   public void testSecond() {    System.out.println("second Test Case...");   }     /**    * 测试方法3    */   public void testThird() {    System.out.println("third Test Case...");   }     }

测试结果如下:

second Test Case...  third Test Case...  first Test Case...

2.Junit4.x版本以后的测试方法:

package com.self;
import org.junit.After;  import org.junit.AfterClass;  import org.junit.Before;  import org.junit.BeforeClass;  import org.junit.Test;
public class TestPractise {     public static int i;   public static int j;     public TestPractise() {    super();   }     /**    * 只调用一次,在所有测试方法执行前调用,可做些准备工作    */   @BeforeClass   public static void beforeClass() {    System.out.println("this method is called only One Time before...");   }     /**    * 每个测试方法执行前都调用    */   @Before   public void beforeMethod() {    System.out.println("this method is called EveryTime before..." + i++);   }     /**    * 测试方法1    */   @Test   public void firstMethod() {    System.out.println("first Test Case...");   }     /**    * 测试方法2    */   @Test   public void secondMethod() {    System.out.println("second Test Case...");   }     /**    * 每个测试方法执行后都调用    */   @After   public void afterMethod() {    System.out.println("this method is called EveryTime end..." + j++);   }     /**    * 只调用一次,在所有测试方法执行结束后调用,可做些清理工作    */   @AfterClass   public static void afterClass() {    System.out.println("this method is called only One Time after...");   }    }

测试结果如下:

this method is called only One Time before...  this method is called EveryTime before...0  first Test Case...  this method is called EveryTime end...0  this method is called EveryTime before...1  second Test Case...  this method is called EveryTime end...1  this method is called only One Time after...

备注:以上标红的两处,即@BeforeClass,@AfterClass必须是静态方法

其他知识可参考以下两处:

JUnit学习笔记:http://wanqiufeng.blog.51cto.com/409430/426763

全面认识JUnit4的新特特征:http://dev.yesky.com/375/2584375.shtml

转自:http://binglimeng.iteye.com/blog/1416556