A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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

import static org.junit.Assert.*;

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

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

@RunWith(Parameterized.class)
public class ParameterTest {
        int expected =0;
        int input1 = 0;
        int input2 = 0;
       
        @Parameters
        public static Collection<Object[]> t() {
                return Arrays.asList(new Object[][]{
                                {3,1,2},
                                {4,2,2}
                }) ;
        }
       
        public ParameterTest(int expected,int input1,int input2) {
                this.expected = expected;
                this.input1 = input1;
                this.input2 = input2;
        }
       
        @Test
        public void testAdd() {
                assertEquals(expected, new Calculate().add(input1, input2));
        }

}


---------------------
【转载,仅作分享,侵删】
作者:若海音符
原文:https://blog.csdn.net/Chenyt01/article/details/51445367
版权声明:本文为博主原创文章,转载请附上博文链接!

1 个回复

倒序浏览
奈斯,感谢分享!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马