使用Java JUnit框架里的@SuiteClasses注解管理测试用例

发布时间:2020-08-13 13:43:08

来源:ITPUB博客

阅读:96

作者:i042416

Suppose you need to repeatedly execute some test method in your unit test case, for example, you would like to test getPrice based on the first set of test data 5 times in test method test1() while for the second set of test data, only one time should be executed.

The below class RepeatDemoOne is a bad example, where this special LOOP operation is mixed with test method implementation.

Ideally the test method should only contain the pure logic to operate on the method being tested. So we have a better solution RepeatDemoTwo: It could easily be observed that now the test method test1 and test2 are rather clean: no more for LOOP and System.out.println exist any more.

Instead, I put the LOOP logic and print out operation into class RepeatableRule which implements interface MethodRule. The concrete rule implementation is done by overriding method apply as below:

class RepeatableRule implements MethodRule{

int times = 1;

String[] testMethods = null;

RepeatableRule(int times, String[] testMethods){

this.times = times;

this.testMethods = testMethods;

}

@Override

public Statement apply(final Statement base, final FrameworkMethod method, Object target) {

return new Statement() {

@Override

public void evaluate() throws Throwable {

int loopTime = 1;

if(Arrays.asList(testMethods).contains(method.getName())) {

loopTime = times;

}

for(int i = 0; i

base.evaluate();

System.out.println(method.getName() + " executed.");

}

}

};

}  }

When I execute this test case, I can get exactly the same result as RepeatDemoOne:

With the help of @Rule, we can achieve the same as @Test(expected=).

For example, we can use an instance of class ExpectedException to manually declare within a test method itself that a test method expects a given type of exception class.

Besides exception, we can also manually specify a sub string which is expected to appear in an error message, and add our custom error message in Junit report if a test method fails. See following code for example:

public class RuleWithException {

@Rule

public ExpectedException exp = ExpectedException.none();

@Test

public void expectMessage()

{

exp.expectMessage("Hello World");

throw new RuntimeException("Hello World will throw exception.");

}

@Test

public void expectCourse()

{

exp.expectCause(new BaseMatcher()

{

public boolean matches(Object item)

{

return item instanceof IllegalArgumentException;

}

@Override

public void describeTo(org.hamcrest.Description description) {

description.appendText("Expected exception with type IllegalArgumentException "

+ "raised in test method! ");

}

});

Throwable cause = new IllegalArgumentException("Cause Test.");

throw new RuntimeException(cause);

}}

In this example, if we comment out line 46, the customed message defined in method describeTo will be printed out in JUnit console:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

用java里的junit框架_使用Java JUnit框架里的@SuiteClasses注解管理测试用例相关推荐

  1. 【源码+图片素材】Java王者荣耀游戏开发_开发Java游戏项目【王者荣耀】1天搞定!!!腾讯游戏_Java课程设计_Java实战项目_Java初级项目

    王者荣耀是当下热门手游之一,小伙伴们是否想过如何制作一款属于自己的王者荣耀游戏呢? 本课程讲解了一个王者荣耀游戏的详细编写流程,即使你是刚入门Java的新手,只要你简单掌握了该游戏所需要的JavaSE ...

  2. java 银行框架_一般Java做银行项目使用什么技术啊?

    如果是做BS开发的话.一般需要java web相关技术. 一般的学习顺序为:html,javascript,servlet,jsp,还有主流的ssh框架. JavaEE: 第一阶段Java语言基础: ...

  3. java写抽象类的测试_如何使用JUnit在Java中测试抽象类?

    如何使用JUnit在Java中测试抽象类? 我是JUnit的Java测试新手. 我必须使用Java,我想使用单元测试. 我的问题是:我有一个带有一些抽象方法的抽象类. 但是有些方法并不是抽象的. 如何 ...

  4. java建站集成包_基于Java的快速建站框架实现(含源文件).doc

    武汉纺织大学2010届毕业设计论文 PAGE \* MERGEFORMAT 16 1 概述 1.1 课题背景 随着IT技术和互联网应用的蓬勃发展,类似Blog.BBS等类型的个人型网站的需求量也越来越 ...

  5. java mysql orm框架_主流 Java ORM 框架有哪些?

    主流 Java ORM 框架有哪些? ORM 是 Object Relational Mapping 的缩写,译为 "对象关系映射" 框架. 所谓的 ORM 框架就是一种为了解决面 ...

  6. java 轻量级插件化框架_轻量级插件化框架——Small

    photo-1441716844725-09cedc13a4e7.jpg 前言 世界那么大,组件那么小.Small,做最轻巧的跨平台插件化框架. --Galenlin 这是Small作者,林光亮老师, ...

  7. java 取栈顶元素_《Java实战之内存模型》详解篇

    内存是非常重要的系统资源,是硬盘和CPU的中间仓库及桥梁,承载着操作系统和应用程序的实时运行 JVM内存布局规定了Java在运行过程中内存申请.分配.管理的策略,保证了JVM的高效稳定运行 不同的JV ...

  8. java 字节码增强原理_深入浅出Java探针技术1--基于java agent的字节码增强案例

    Java agent又叫做Java 探针,本文将从以下四个问题出发来深入浅出了解下Java agent 一.什么是java agent? Java agent是在JDK1.5引入的,是一种可以动态修改 ...

  9. java 多态判断非空_收藏Java 面试题全梳理

    脚本之家 你与百万开发者在一起 来源 | Java建设者(ID:javajianshe) 作者 |cxuan 如若转载请联系原公众号 Java 基础篇 Java 有哪些特点 并发性的:你可以在其中执行 ...

最新文章

  1. 我要做 Android 之面笔试总结
  2. Android 自定义dialog
  3. mysql profiles清空_MYSQL 使用show profiles 分析性能
  4. CreateFile
  5. 21天早起计划,奖你1000+元!
  6. 常用机器学习算法优缺点及其应用领域
  7. 直播录屏软件哪个好?什么软件可以录屏直播会议?
  8. 【微信小程序】解决代码上传超过大小限制
  9. linux脚本菜鸟教程,菜鸟教程之shell _1
  10. STM32F407读取ADS1115数据
  11. 矩阵论(四)——矩阵的广义逆
  12. 托宾Q数据-沪深A股上市公司(含行业名称、代码等指标)2003-2020
  13. win10需要修复计算机,Win10开机蓝屏,显示“你的电脑/设备需要修复”怎么办?答案来了...
  14. NVP6124I北京冠宇铭通 芯片
  15. html5 qq看点,AlloyTouch实战--60行代码搞定QQ看点资料卡
  16. java webp文件转png,jpg
  17. 记一次机器内存过高引起的线上故障
  18. mac nexus 资源下载及安装
  19. spring_定时任务时间设置详解
  20. 机器人导航技术的研究现状和发展趋势

热门文章

  1. java 值传递 引用传递_Java小白进阶之值传递-引用传递
  2. 视觉SLAM——ORB-SLAM2运行tum数据集,kitti数据集,euroc数据集
  3. C++新特性探究(十二):static_assert(提前判误)
  4. frm文件导入mysql5.7_使用frm,ibd文件恢复数据库文件,mysql5.7.31,centos7.5,20200813...
  5. svn怎么检出两个地址项目_svn 检出项目报错
  6. 去除dataframe中的空行_Python数据分析中的处理与数值加速技巧简介
  7. linux无法将文件移入回收站,linux中使用rm命令将文件移到回收站的方法
  8. 编程完数_初级编程C++题:11H1343: 完数
  9. java 不定参数_Java不定参数
  10. vue 指令 v-once