美文网首页
Parameterized 参数化

Parameterized 参数化

作者: 不步步不 | 来源:发表于2020-05-21 09:16 被阅读0次
packagecn.itcast.test;

importcn.itcast.junit.Calculate;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.junit.runners.Parameterized;

importjava.util.Arrays;
importjava.util.Collection;

importstaticorg.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
publicclassParameterTest{
/*
*1更改默认的测试运行器为RunWith(Parameterized.class)
*2声明变量来存放预期值和结果值
*3声明一个返回值为Collection的公共静态方法,并使用@Parameters进行修饰
*4为测试类声明一个带有参数的公共构造函数,并在其中为之声明变量赋值
**/
intexpected=0;
intinput1=0;
intinput2=0;

@Parameterized.Parameters
publicstaticCollection<Object[]>t(){
returnArrays.asList(newObject[][]{
{3,1,2},
{4,2,2}
});
}

publicParameterTest(intexpected,intinput1,intinput2){
this.expected=expected;
this.input1=input1;
this.input2=input2;
}

@Test
publicvoidtestAdd(){
assertEquals(expected,newCalculate(input1,input2).add());
}
}

运行结果:


image.png

相关文章

网友评论

      本文标题:Parameterized 参数化

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