junit中的@classrule,@rule

junit中的@classrule,可以在所有类方法开始前进行一些初始化调用,比如创建临时文件,

package com.jdriven;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;

public class JUnitClassRuleTest {


@ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder();

public static File tempFile;

@BeforeClass
public static void createTempFile() throws IOException {
tempFile = temporaryFolder.newFile("tempFile.txt");
}

@Test
public void testJUnitClassRule_One() {
//Your test should go here, which uses tempFile
}

@Test
public void testJUnitClassRule_Two() {
//Your test should go here and uses the same tempFile
}
}



其中,@ClassRule中指定创建临时文件夹,这是在所有的测试方法前会创建文件夹,并且会在所有测试完成后,递归删除其下的子目录和子文件夹。
@Rule是方法级别的,每个测试方法执行时都会调用被注解的Rule,而@ClassRule是类级别的,在执行一个测试类的时候只会调用一次被注解的Rule

------------------------------------------------------------
再看下其他内置的rule
ExternalResource Rule
ExternalResource 是TemporaryFolder的父类,主要用于在测试之前创建资源,并在测试完成后销毁。
Java代码 收藏代码
File tempFile;

@Rule
public ExternalResource extResource = new ExternalResource() {
//每个测试执行之前都会调用该方法创建一个临时文件
@Override
protected void before() throws Throwable {
tempFile = File.createTempFile("test", ".txt");
}

//每个测试执行之后都会调用该方法删除临时文件
@Override
protected void after() {
tempFile.delete();
}
};

@Test
public void testExtResource() throws IOException {
System.out.println(tempFile.getCanonicalPath());
}

ErrorCollector Rule
ErrorCollector允许我们收集多个错误,并在测试执行完后一次过显示出来
Java代码 收藏代码
@Rule
public ErrorCollector errorCollector = new ErrorCollector();

@Test
public void testErrorCollector() {
errorCollector.addError(new Exception("Test Fail 1"));
errorCollector.addError(new Throwable("fff"));
}

Verifier Rule
Verifier是ErrorCollector的父类,可以在测试执行完成之后做一些校验,以验证测试结果是不是正确
Java代码 收藏代码
String result;

@Rule
public Verifier verifier = new Verifier() {
//当测试执行完之后会调用verify方法验证结果,抛出异常表明测试失败
@Override
protected void verify() throws Throwable {
if (!"Success".equals(result)) {
throw new Exception("Test Fail.");
}
}
};

@Test
public void testVerifier() {
result = "Fail";
}

TestWatcher Rule
TestWatcher 定义了五个触发点,分别是测试成功,测试失败,测试开始,测试完成,测试跳过,能让我们在每个触发点执行自定义的逻辑。
Java代码 收藏代码
@Rule
public TestWatcher testWatcher = new TestWatcher() {
@Override
protected void succeeded(Description description) {
System.out.println(description.getDisplayName() + " Succeed");
}

@Override
protected void failed(Throwable e, Description description) {
System.out.println(description.getDisplayName() + " Fail");
}

@Override
protected void skipped(AssumptionViolatedException e, Description description) {
System.out.println(description.getDisplayName() + " Skipped");
}

@Override
protected void starting(Description description) {
System.out.println(description.getDisplayName() + " Started");
}

@Override
protected void finished(Description description) {
System.out.println(description.getDisplayName() + " finished");
}
};

@Test
public void testTestWatcher() {
/*
测试执行后会有以下输出:
testTestWatcher(org.haibin369.test.RulesTest) Started
Test invoked
testTestWatcher(org.haibin369.test.RulesTest) Succeed
testTestWatcher(org.haibin369.test.RulesTest) finished
*/
System.out.println("Test invoked");
}

TestName Rule
TestName能让我们在测试中获取目前测试方法的名字。
Java代码 收藏代码
@Rule
public TestName testName = new TestName();

@Test
public void testTestName() {
//打印出测试方法的名字testTestName
System.out.println(testName.getMethodName());
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值