https://www.cnblogs.com/memory4young/p/junit-testsuite.html

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

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

如何使用呢 ?

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

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 }

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

TestSuite 该上场了。

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

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 }

先运行一下,看下效果:

虽然这个测试类写了增(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 ,代码如下:

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 }

运行后,效果如下:

详细代码可参考:

java testsuite_JUnit —— TestSuite 的使用相关推荐

  1. java testsuite,JUnit之TestCase和TestSuite详解

    Android Studio下单元测试的本质其实是根据通过书写JAVA测试代码,通过模拟用户调用相应的方法,或者使用者按下相应的按键来验证我们的代码的逻辑是否能达到预期的要求,如果所有的用例都能通过, ...

  2. java单元测试启动类配置_Springboot 单元测试简单介绍和启动所有测试类的方法

    最近一段时间都是在补之前的技术债,一直忙着写业务代码没有注重代码的质量,leader也在强求,所有要把单元测试搞起来了 我把单元测试分为两种 一个是service的单元测试,一个是controller ...

  3. Java单元测试之JUnit4详解

    2019独角兽企业重金招聘Python工程师标准>>> Java单元测试之JUnit4详解 与JUnit3不同,JUnit4通过注解的方式来识别测试方法.目前支持的主要注解有: @B ...

  4. 走进Java接口测试大门之测试框架TestNG

    一.简介 TestNG 是一个受 JUnit 和 NUnit 启发的测试框架,旨在简化广泛的测试需求,从单元测试到接口测试. 但引入了一些新功能,使其更强大,更易于使用,例如: 注释. 在线程池中运行 ...

  5. java实现的18位×××格式验证算法

    公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码.1.地址码表示编码对象常住户口所在县(市. ...

  6. java单元测试总结

    java单元测试(使用junit) http://www.cnblogs.com/feiweiwei/archive/2009/06/16/1024623.html     JUnit是由 Erich ...

  7. java commons-chain_Apache commons chain 初探

    Apache commons chain 是什么 Apache common chain 是对责任链设计模式的改造封装,让使用者更加方便的使用. 简单回顾一下责任链设计模式 在阎宏博士的<JAV ...

  8. cactus java,使用cactus实现对servlet进行单元测试

    使用cactus实现对servlet进行单元测试 步骤如下: [1]创建Web工程ServletTestDemo 在myeclipse中创建Web project,命名为ServletTestDemo ...

  9. Java单元测试(Junit+Mock+代码覆盖率)

    单元测试是编写测试代码,用来检测特定的.明确的.细颗粒的功能.单元测试并不一定保证程序功能是正确的,更不保证整体业务是准备的. 单元测试不仅仅用来保证当前代码的正确性,更重要的是用来保证代码修复.改进 ...

最新文章

  1. LeetCode--448
  2. SQL Server导入导出工具弱爆了
  3. php return 变量,php内核笔记–函数返回变量return_value
  4. url中传递对象参数_在URL参数中传递复杂对象
  5. jJMeter UDP Request:不等待服务器响应
  6. hadoop集群配置SSH免登陆
  7. 【Proteus仿真8086】简单IO接口实验——无条件传输和查询方式
  8. 安装配置文件共享协议(SAMBA)
  9. IDE工具的[多行光标编辑模式]
  10. 解决办法:undefined reference to symbol 'dlclose@@GLIBC_2.2.5'
  11. 20160205 - Windows 10 家庭版没有组策略
  12. picasa android 缓存,从android应用上传到picasa
  13. 室内外一体化融合定位技术浅析--室内定位--新导智能
  14. I.MX RT1176笔记(3)-- 双核启动和通信 MU
  15. sharepoint 服务器错误: http://go.microsoft.com/fwlink?LinkID=96177
  16. hbuilder运行uniapp,微信开发者工具打开但没有运行项目
  17. 从零开始学视觉里程计——一个初学者教程
  18. 量子计算与量子软件(一)
  19. 数组中map遍历会改变原数组吗?
  20. 用construct2做一个酷炫到爆炸的海绵宝宝大战痞老板游戏

热门文章

  1. nyoj1016德莱联盟【判断两线段是否相交】
  2. 腾讯2018秋招正式笔试题目——拼凑硬币
  3. 阿里云域名配置以及https证书(ssl证书)配置
  4. Windows8.1安装tools提示:安装程序无法验证是否已安装所需的Microsoft更新KB2919355的问题详解
  5. javafx 教程_Swing和JavaFX:使用JFXPanel
  6. 华为复制加密门禁卡_将多种累赘门禁卡归一合并的最佳选择
  7. inferred type_您最终可以使用var在Java中声明Inferred Type局部变量-这就是为什么它很棒...
  8. vue 中实现动态切换背景图
  9. Iphone8 plus系统照片为什么电脑打不开 打开heic文件教程
  10. oracle查询注意点,Oracle_spatial的常见错误与注意事项