简体   繁体   English

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

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

Parameterized junit test can be written to test for multiple values. 可以编写参数化的junit测试来测试多个值。 In this case what should be in the expected value? 在这种情况下,期望值应该是多少? I'm storing the values to be tested as list, and the elements in the list are the candidates for the test data. 我将要测试的值存储为列表,列表中的元素是测试数据的候选项。

 @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));
    }
}

Although this works, to calculate expected value, it is calling the same method. 尽管这可行,但是要计算期望值,它正在调用相同的方法。 I want to store the expected values and then check them against the actual values. 我想存储期望值,然后对照实际值进行检查。 How does it need to be done? 需要如何做?

The parameters need to contain the inputs, but also the expected output: 参数需要包含输入,还需要包含预期的输出:

@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