您的位置:首页 > 其它

junit4 参数化测试 (惭愧,工作了这么多年都没用过这功能!)

2012-05-18 18:43 211 查看
package com.yy.redis;

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

import junit.framework.Assert;

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

/**
* @author zhaoming23@gmail.com
* 2012-5-18 下午06:34:12
*/

@RunWith(Parameterized.class)
public class ParamTest {

private int input;
private int output;

public ParamTest(int input, int output) {
this.input = input;
this.output = output;
}

/** 这个方法必须是叫data 而且是public static的类型 */
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{1, 1},
{2, 4},
{3, 9},
{-3, 9},

});
}

@Test
public void test() {
int square = Calc.square(input);
Assert.assertEquals(output, square);
}

private static class Calc {
public static int square(int x) {
return x * x;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: