我有这个Java文件从用户接收两个int并打印出数字的总和,这个文件叫做JavaAdd.java。要测试此文件,我需要创建一个可以包含可能的测试编号的文件,称为JavaAddTest。当我创建JavaAddTest时,第一行看起来像:
2 2
当我运行脚本时:
java JavaAdd < JavaAddTest
我得到输出到控制台:
Please input 2 ints:
The sum of the 2 values are: 4
这就是我想要的。但我希望能够用一组数字填充JavaAddTest,如:
2 2
0 2
-2 -2
10 10
并且能够获得如下输出:
Please input 2 ints:
The sum of the 2 values are: 4
Please input 2 ints:
The sum of the 2 values are: 2
Please input 2 ints:
The sum of the 2 values are: -4
Please input 2 ints:
The sum of the 2 values are: 20
我可以在linux环境中执行此操作吗?我需要做什么来使JavaAddTest能够读取我的所有输入?
答案 0 :(得分:0)
JUnit tutorial link -> see Parameterized test in it for Junit
public class JavaAdd {
public void add(int a, int b) {
System.out.println("The sum is " + (a + b));
}
}
Junit课程
@RunWith(Parameterized.class)
public class JavaAddTest {
int a, b;
public JavaAddTest(int a, int b) {
this.a = a;
this.b = b;
}
@Parameterized.Parameters
public static Collection addNumberInfo() {
// if you want to read the numbers from file.
// read here and create array and return it.
return Arrays
.asList(new Integer[][] { { 2, 2 }, { 3, 4 }, { 19, 5 }, { 22, 6 }, { 23, 7 } });
}
@Test
public void testAdd() {
JavaAdd add = new JavaAdd();
add.add(a, b);
}
}
输出以下代码
The sum is 4
The sum is 7
The sum is 24
The sum is 28
The sum is 30
希望这就是你要找的东西。