【发布时间】:2017-09-09 21:43:22
【问题描述】:
我正在尝试为计算矩阵对角线之和差异的小函数编写一个测试用例(使用 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);
}
}
当我运行它时,我得到一个错误“方法测试应该没有参数。”
我的问题是:
- 如何为这种情况编写测试用例?
- 如何准备测试数据?
- 我们能否在资源文件夹中创建一个类似于属性类型的文件,以便它也可以提供给其他功能。
【问题讨论】:
标签: java eclipse unit-testing junit test-data