美文网首页程序员JAVA_UT
【JAVA-UT】9、Rule--个体的规范

【JAVA-UT】9、Rule--个体的规范

作者: 码术张 | 来源:发表于2018-03-25 18:24 被阅读109次

文|码术张

一、如何使用Rule

下面这个测试类TimeoutTest,有两个Test方法,分别在打印一个字母后,睡眠2000毫秒和4000毫秒。

public class TimeoutTest {
  @Test
  public void should_timeout1() throws InterruptedException {
    System.out.println("A...");
    Thread.sleep(2000);
  }

  @Test
  public void should_timeout2() throws InterruptedException {
    System.out.println("B...");
    Thread.sleep(4000);
  }
}

后来公司要求,每个Test方法运行时间不能超过3000毫秒,如果超过了3000毫秒,就算失败。

如何实现这一功能?

需两步:

  1. 获得一个定时器对象

public TestRule timeout = Timeout.millis(3000);

  1. 使用Rule注解
@Rule
public TestRule timeout = Timeout.millis(3000);

修改后,测试类如下:

public class TimeoutTest {
  @Rule
  public TestRule timeout = Timeout.millis(3000);

  @Test
  public void should_timeout1() throws InterruptedException {
     System.out.println("A...");
     Thread.sleep(2000);
  }

  @Test
  public void should_timeout2() throws InterruptedException {
     System.out.println("B...");
     Thread.sleep(4000);
  }
}

运行结果为:
睡眠2000ms的Test方法成功,
睡眠4000ms的Test方法should_timeout2失败。


1541388504275.png

为什么呢?

因为本例子中,Rule限制了每一个Test方法运行时间不能超过3000ms。

可以看到Rule是如何使用的:

1541388841122.png

注意:

  1. 所要注解的对象必须声明为public。
  2. Rule只能注解TestRule类型的对象。

本例中Timeout类型是TestRule的子类型。

如果非TestRule类型或其子类型,运行时就会报错。

例如用Rule注解String类型对象。

public class TimeoutTest {
  @Rule
  public String name = "abc";

  @Test
  public void should_timeout1() throws InterruptedException {
    System.out.println("A...");
    Thread.sleep(4000);
  }
}

运行结果如下,提示@Rule要注解MethodRule或TestRule(实际上,MethodRule已经被TestRule代替了)。


1539843982099.png
二、 3种TestRule类的功能介绍

有哪些TestRule类呢?

当前,Junit4提供了8类TestRule:

ErrorCollector
ExpectedException
ExternalResource
TemporaryFolder
TestName
TestWatcher
Timeout
Verifier

下面介绍常用的3种。

  1. Timeout

​ Timeout规定每个test方法的可运行时间,如果这么想,就错了。
​ Timeout只是设置了一个定时器,并不规定每个test方法的运行时间。
​ 这定时器被Rule拿来使用,以达到规定每个test方法运行时间的目的。

  1. TestName

    获取当前运行的方法的名字。

    public class Sample {
      @Rule
      public TestName name = new TestName();
    
      @Test
      public void John() {
        System.out.println("my name is: " + name.getMethodName());
      }
    }
    

运行结果为:


1539929275418.png
  1. TemporaryFolderTemporaryFolder
    它是一个用于创建、删除临时文件或文件夹的工具。
    但何时创建、销毁,并不是这个工具的功能,而是Rule的功能。

    下面举例说明,如何创建文件。

public class RuleTest {
  @Rule
  public TemporaryFolder tmpFolder = new TemporaryFolder();

  @Test
  public void should_existFile() throws IOException {
    // when
    File file = tmpFolder.newFile("myfile.txt");

    // then
    assertTrue(file.isFile());
  }
}

运行中,Junit会自动创建一个文件夹junit2973493844983643175,创建的文件就在这一文件夹中。
运行结束,这个文件夹junit2973493844983643175及其中的文件会自动删除。


1540791154867.png 1540791187281.png

相关文章

  • 【JAVA-UT】9、Rule--个体的规范

    文|码术张 一、如何使用Rule 下面这个测试类TimeoutTest,有两个Test方法,分别在打印一个字母后,...

  • 【JAVA-UT】8、After--个体的尾巴

    文|码术张 如果公共变量的值,在每个test运行后,需要归零,那么可以使用After。 After就是在每个tes...

  • 个体暴力的来源

    个体暴力的来源是什么?这是个值得深思的问题。当人们没有各种法则规范时,就是丛林法则,弱肉强食的自然规律,个体暴力来...

  • 人力资源管理的六个角度

    掌握每个体系中的所有内容,并按照体系中的要求和标准进行规范化管理,就可实现“人人有事干,事事有规范,办事...

  • 性别图示最小化的策略

    性别图式(gender schema)是指个体按照社会上有关区别男女性别特征的规范所形成的认知结构。个体据此组织和...

  • 服从

    人生伴随着服从。服从,个体在社会要求、群体规范或他人意志的压力下,被迫产生的符合他人或规范要求的行为。 在我...

  • 规范

    《规范》 文/六悦 一些习惯一旦养成,就意味着确立了一种规范,这规范适用于个人也适用于各种企事业单位个体经营...

  • 读 超级个体#9

    赵昂 | 别傻了,哪有什么职业定位? 前面几期都说了职业定位相关的内容,但是这次古典请来的同行大咖赵昂老师却说,哪...

  • 《超级个体》学习9

    碎片化时代,三种能力让效率升级。 我们先了解一下时间流这个概念。在历史中有三种这样的时间,一个是农业时代的时间流,...

  • 9/52超级个体

网友评论

    本文标题:【JAVA-UT】9、Rule--个体的规范

    本文链接:https://www.haomeiwen.com/subject/pwzhcftx.html