Junit4 -- TestSuite 的使用

看到此文章对Junit的TestSuite讲解比较明了,特转载至此,以供参考和记忆:

首先说一下,suite ,中文是 一套,一组 的意思。

那么,TestSuite,顾名思义,就是用来运行一组测试的。

如何使用呢 ?

 假设有个这样的测试类 StudentDAOTest ,代码如下:

Junit4 -- TestSuite 的使用
 1 package org.young.junit.testsuite;
 2 
 3 import junit.framework.TestCase;
 4 
 5 import org.young.junit.testsuite.dao.StudentDAO;
 6 import org.young.junit.testsuite.dao.StudentDAOImpl;
 7 import org.young.junit.testsuite.entity.Student;
 8 
 9 public class StudentDAOTest extends TestCase {
10     
11     private StudentDAO dao;
12     
13     /**
14      * 创建 DAO 实例
15      */
16     public void setUp() {
17         dao = new StudentDAOImpl();
18     }
19 
20     public void testAdd() {
21         Student stu = new Student();
22         
23         dao.add(stu);
24     }
25 
26     public void testDelete() {
27         
28         dao.delete("id");
29     }
30 
31     public void testUpdate() {
32         Student stu = new Student();
33         
34         dao.update(stu);
35     }
36 
37     public void testLoadWithId() {
38         
39         Student stu = dao.load("xyz");
40         
41         assertNotNull(stu);
42     }
43     
44     public void testLoadWithNullOrEmptyStr() {
45         
46         Student stu = dao.load("");
47         assertNull(stu);
48         
49         stu = dao.load(null);
50         assertNull(stu);
51     }
52 
53 }
Junit4 -- TestSuite 的使用

  如果想一次执行几个方法,而不是所有方法改怎么办呢?

 TestSuite 该上场了。

为了方便比较,再来一个测试类 CourseDAOTest ,代码如下:

Junit4 -- TestSuite 的使用
 1 package org.young.junit.testsuite;
 2 
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 import org.young.junit.testsuite.dao.CourseDAO;
 6 import org.young.junit.testsuite.dao.CourseDAOImpl;
 7 import org.young.junit.testsuite.entity.Course;
 8 
 9 import junit.framework.TestCase;
10 import junit.framework.TestSuite;
11 
12 /**
13  * Suite 的实现方式一
14  * 
15  *     public static Test suite(){} 的方式
16  *
17  * @author by Young.ZHU
18  *        on 2013-9-30
19  *
20  * Package&FileName: org.young.junit.testsuite.CourseDAOTest
21  */
22 public class CourseDAOTest extends TestCase {
23     
24     private CourseDAO dao;
25     
26     public CourseDAOTest() {
27         super();
28     }
29 
30     public CourseDAOTest(String name) {
31         super(name);
32     }
33 
34     @Override
35     protected void setUp() throws Exception {
36         super.setUp();
37         
38         dao = new CourseDAOImpl();
39     }
40     
41     /**
42      * 注意:继承 TestCase 后,JUnit 4 里的 @Before 、@Test 等注解就没用了
43      * 
44      * @Before 的功能可由方法 setUp() 实现
45      */
46     @Before
47     public void init() {
48         System.out.println("fsdfsdf");
49         dao = new CourseDAOImpl();
50     }
51     
52     /**
53      * 执行这个测试类的部分方法
54      * 
55      * 方法头必须是这样的 public static junit.framework.Test suite()
56      * 即,静态(static) 的
57      * 
58      * @return
59      */
60     public static junit.framework.Test suite() {
61         TestSuite suite = new TestSuite();
62         
63         /*
64          * 字符串参数为想要执行的该测试类的方法
65          */
66         suite.addTest(new CourseDAOTest("testLoad"));
67         suite.addTest(new CourseDAOTest("testAdd"));
68         
69         return suite;
70     }
71 
72     @Test
73     public void testAdd() {
74         Course course = new Course();
75         
76         dao.add(course);
77     }
78 
79     @Test
80     public void testDelete() {
81         fail("Not yet implemented");
82     }
83 
84     @Test
85     public void testUpdate() {
86         fail("Not yet implemented");
87     }
88 
89     @Test
90     public void testLoad() {
91         Course course = dao.load("course_id");
92         
93         assertNotNull(course);
94     }
95 
96 }
Junit4 -- TestSuite 的使用

 先运行一下,看下效果:

Junit4 -- TestSuite 的使用

 

虽然这个测试类写了增(add)、删(delete)、改(update)、查(load),但实际执行的只有两个方法 —— testLoad 和 testAdd 。

秘密就在于代码第 60 行的 suite() 方法,这个方法决定了该测试类执行哪些方法。

 有两点需要说明:

1、关于方法 suite() 的方法头

正如注释里写道的,这个方法的方法头是固定的 

1 public static junit.framework.Test suite() {
2        // your code ...  
3 }

 2、测试类的构造方法

测试类 CourseDAOTest  中第 30 行带参数的构造函数,在 66 行和 67 行用到了。

构造函数的参数即要执行的测试方法的名称。

 最后,把两个集合起来看,测试类 AllTest ,代码如下:

Junit4 -- TestSuite 的使用
 1 package org.young.junit.testsuite;
 2 
 3 import junit.framework.TestSuite;
 4 
 5 
 6 public class AllTest {
 7 
 8     public static junit.framework.Test suite() {
 9         TestSuite suite = new TestSuite("All Test");
10         
11         /*
12          * StudentDAOTest 类的全部测试方法
13          */
14         suite.addTest(new TestSuite(StudentDAOTest.class));
15         /*
16          * CourseDAOTest 类的部分方法
17          */
18         suite.addTest(CourseDAOTest.suite());
19         
20         return suite;
21     }
22 
23 }
Junit4 -- TestSuite 的使用

 

运行后,效果如下:

Junit4 -- TestSuite 的使用

 

详细代码可参考:

https://github.com/YoungZHU/CollectionCode4Java/tree/master/test/org/young/junit/testsuite


转载自:http://www.cnblogs.com/memory4young/p/junit-testsuite.html