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

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; 
        }
    }
    
    
}
posted @ 2012-05-18 18:43  迷离雅琪  阅读(355)  评论(0编辑  收藏  举报