四、Junit参数化设置

package com.myz.util;

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 JunitParameterTest {
    
    /**
     * 1.更改默认的运行器为@RunWith(Parameterized.class)
     * 2.声明变量来存放预期值和结果值
     * 3.声明一个返回值为collection的公共静态方法,并使用@Parameters进行修饰
     * 4.为测试类声明一个带参数的公共构造函数,并在其中为之声明变量赋值
     */
    
    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 JunitParameterTest(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));
    }
}

 

posted @ 2018-02-24 13:53  菜鸡蔡文姬  阅读(490)  评论(0编辑  收藏  举报