有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何准备和提供测试数据

我正在尝试为计算矩阵对角线和之差的小函数编写一个测试用例(使用Java、Maven、Junit、Eclipse)

要测试的功能

    public static int diagonalDifference(Integer matrix[][], int n) {
        int diagonalSum1 = 0;
        int diagonalSum2 = 0;
        int diagonalDifference = 0;

        for (int i = 0; i < n; i++) {
            diagonalSum1 = diagonalSum1 + matrix[i][i];
            diagonalSum2 = diagonalSum2 + matrix[i][Math.abs(i - 2)];
        }

        diagonalDifference = Math.abs(diagonalSum1 - diagonalSum2);
        return diagonalDifference;
    }
}

从这个答案来看https://stackoverflow.com/a/19349565/7115684我没有成功地尝试

public class testSolutions {

    Solution solution = new Solution();

    Integer a[][] = { { 11, 2, 4 }, { 4, 5, 6 }, { 10, 8, -12 } };
    Integer b[][] = { { 12, 22, 8 }, { 2, 16, 8 }, { 10, 5, -1 } };

    @DataProvider
    public Object[][] provideMatrixAndExpectedSum() {
         return new Object[][] { { a, new Integer(15) } };
    }

    @Test
    @UseDataProvider("provideMatrixAndExpectedSum")
    public void test(Integer a[][], int n) {

        int diagonalDifference = Solution.diagonalDifference(a, n);

        assertEquals(diagonalDifference, 15);
    }
}

当我运行这个时,我得到一个错误“方法测试应该没有参数。”

我的问题是:

  1. 如何为这种情况编写测试用例
  2. 如何准备测试数据
  3. 我们可以在资源文件夹中创建一个类似文件的属性类文件,以便也可以将其提供给其他函数

共 (2) 个答案

  1. # 1 楼答案

    似乎要测试的函数接受一个2d数组并返回一个整数,您需要测试返回的值是否正确。 我有一个类似的代码和使用JUint的测试单元。不要为我的代码做什么而烦恼,只关注传递的输入和要验证的输出

    public class countUniqueNums {
    
    public int countUnique (int[] nums) {
        int unique=0;
        int [] intVal = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        for (int i = 0; i<nums.length; i++) {
            intVal[nums[i]+9]++;
            if (intVal[nums[i]+9]==1)
                unique++;
            else
                if (intVal[nums[i]+9]==2)
                    unique ;
        }
        return unique;
     }
    }
    

    测试单元为:

    import static junitparams.JUnitParamsRunner.$;
    import static org.junit.Assert.assertEquals;    
    import org.junit.Test;
    import org.junit.runner.RunWith;    
    import junitparams.JUnitParamsRunner;
    import junitparams.Parameters;
    
    @RunWith(JUnitParamsRunner.class)
    public class countUniqueNumsParamTest {
    
        @SuppressWarnings({ "unused", "deprecation" })
        private static final Object[] countUniqueNumbers () {
            return $(
                    // Parameters are (1,2),  1=expected result count, 2= input array
                        $(1,new int[]{0}),      //Test Case 1
                        $(0,new int[]{}),       //Test Case 2
                        $(0,new int[]{0,0}),    //Test Case 3           
                        $(0,new int[]{0,0,0}),  //Test Case 4
    
                        $(1,new int[]{0,1,1,1}),    //Test Case 5
                        $(1,new int[]{1,1,1,0}),    //Test Case 6
                        $(2,new int[]{1,0,2,1}),    //Test Case 7
                        $(4,new int[]{0,1,2,3})     //Test Case 8
    
                        );
        }
        @Test
        @Parameters(method="countUniqueNumbers")
        public void test(int unique, int[] nums) {
            countUniqueNums cun = new countUniqueNums();
            assertEquals(unique, cun.countUnique(nums));
        }
    }
    

    确保在eclipse中为JUnit导入了正确的库。定制您的测试用例类以匹配我的测试用例类,这样您就可以开始了。 希望这有帮助

  2. # 2 楼答案

    我发现这种方法对于像您这样的用例通常非常令人不安,特别是因为它不是必需的,并且使阅读和理解测试变得更加困难

    为什么没有必要呢?如果我写了一个求和两个整数的方法,我测试1+2=3,为什么我需要写另一个测试来确保2+2=4?你可以问自己的问题是:“我需要在我的代码中更改什么,以便第二次测试(2+2=4)失败但是第一次测试仍然通过?”如果我可以更改生产代码,那么第二个测试失败,但是第一个没有,这意味着它通常是两个独立的测试

    为什么更难理解? 一块

    int summand1 = 1;
    int summand2 = 2;
    
    int sum = MyMath.sum(summand1, summand2);
    
    assertEquals(3, sum);
    

    国际海事组织比国际海事组织更容易理解吗

    public void myTest(int summand1, int summand2, int expectedResult) {
       int sum = MyMath.sum(summand1, summand2);
    
       assertEquals(expectedResult, sum)
    }
    

    如果测试失败,我将在几行代码中获得所有信息。第二,您不知道使用了哪些参数,参数的定义可能在测试类中的某个地方,因此您需要更多的时间来实际找出问题所在