繁体   English   中英

用于在数组中查找最大数量的参数化Junit测试用例的期望值

[英]Expected values for Parameterized Junit test case for finding Max number in an array

可以编写参数化的junit测试来测试多个值。 在这种情况下,期望值应该是多少? 我将要测试的值存储为列表,列表中的元素是测试数据的候选项。

 @RunWith(Parameterized.class)
public class MaxInArrayTest {

    @Parameter
    public int[] a1;

    @Parameters
    public static Collection<int[]> data(){
        int[][] data = new int[][]{{1,2,3,4},{20,30,40,50}};
        return Arrays.asList(data);
    }

    @Test
    public void testMaxInArray(){
        MaxInArray maxInArray = new MaxInArray();
        Assert.assertEquals(maxInArray.findMax(a1), maxInArray.findMax(a1));
    }
}

尽管这可行,但是要计算期望值,它正在调用相同的方法。 我想存储期望值,然后对照实际值进行检查。 需要如何做?

参数需要包含输入,还需要包含预期的输出:

@RunWith(Parameterized.class)
public class MaxInArrayTest {

    private static class ArrayAndExpectedMax {
        private int[] array;
        private int expectedMax;

        public ArrayAndExpectedMax(int[] array, int expectedMax) {
            this.array = array;
            this.expectedMax = expectedMax;
        }

        public int[] getArray() {
            return array;
        }

        public int getExpectedMax() {
            return expectedMax;
        }
    }

    @Parameterized.Parameter
    public ArrayAndExpectedMax param;

    @Parameterized.Parameters
    public static Collection<ArrayAndExpectedMax> data(){
        return Arrays.asList(new ArrayAndExpectedMax(new int[] {1, 2, 3, 4}, 4),
                             new ArrayAndExpectedMax(new int[] {20, 30, 40, 50}, 50));
    }

    @Test
    public void testMaxInArray(){
        MaxInArray maxInArray = new MaxInArray();
        Assert.assertEquals(param.getExpectedMax(), maxInArray.findMax(param.getArray()));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM