junit runwith

junit的runwith是给单元测试中增加一些附属特性,今天在看flink源码的时候,发现其使用了一个注解@RunWith(Parameterized.class),有点好奇,于是研究了一下这个注解的使用。
这个注解产生的背景是这样的:flink中的每个功能要保证在单机模式和集群模式下都能使用,因此如果编写常规的junit test函数,对于每个要测试的功能A,我们需要写两个测试函数AOnSingleMode()AOnClusterMode,这无疑是一种折磨。因此,我们只需要使用注解@RunWith(Parameterized.class),然后在参数@Parameterized.Parameters()定义两种模式:单机模式和集群模式,junit就会默认使用这两种参数去初始化测试类进行测试(因此在测试类中需要一个接收参数的构造函数)。

下面给个实例

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

@RunWith(Parameterized.class)
public class ParameterizedTest {
    public String mode;

    public ParameterizedTest(String mode) {
        this.mode = mode;
    }

    @Test
    public void testParameterized(){
        System.out.println(String.format("当前所属模式:%s", mode));
    }

    @Parameterized.Parameters(name = "execute mode = {0}") // 给这个测试一个名字
    public static Collection<String> executionModes(){
        return Arrays.asList("集群模式",
                "单例模式");
    }
}

测试结果:
在这里插入图片描述


如上面所写,每个测试类需要自己实现public static Collection<String> executionModes()方法,有点烦,为了进一步简化,我们可以构造一个测试类的公共父类,在父类中实现这个方法,然后在每个测试类中继承父类就好了(实际上flink就使用的这种方式):

import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

// 父类
public class CommonTest {
    @Parameterized.Parameters()
    public static Collection<String> executionModes(){
        return Arrays.asList("集群模式",
                "单例模式");
    }
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

// 子类
@RunWith(Parameterized.class)
public class ParameterizedTest extends CommonTest{
    public String mode;

    public ParameterizedTest(String mode) {
        this.mode = mode;
    }

    @Test
    public void testParameterized(){
        System.out.println(String.format("当前所属模式:%s", mode));
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

canaryW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值