我有一个关于使用参数化测试对我的 API 进行单元测试的问题。现在而不是像构建一个数组列表
Arrays.asList(new Object[]{
{1},{2},{3}
});
我想一一读取文件中的行,将它们拆分空间并将它们填充到数组中。这样一来,一切都将被概括。任何人都可以通过示例向我建议任何这样的方法吗?
还有一种方法可以在不将各种参数声明为私有成员并在测试单元的构造函数中初始化它们的情况下进行测试?
编辑:邓肯要求的代码
@RunWith(Parameterized.class)
public class JunitTest2 {
SqlSession session;
Integer num;
Boolean expectedResult;
static BufferedInputStream buffer = null;
public JunitTest2(Integer num, Boolean expected){
this.num = num;
this.expectedResult = expected;
}
@Before
public void setup() throws IOException{
session = SessionUtil.getSqlSessionFactory(0).openSession();
SessionUtil.setSqlSession(session);
buffer = new BufferedInputStream(getClass().getResourceAsStream("input.txt"));
System.out.println("SETUP!");
}
@Test
public void test() {
assertEquals(expectedResult, num > 0);
System.out.println("TESTED!");
}
@Parameterized.Parameters
public static Collection getNum() throws IOException{
//I want my code to read input.txt line by line and feed the input in an arraylist so that it returns an equivalent of the code below
return Arrays.asList(new Object[][]{
{2, true},
{3, true},
{-1, false}
});
}
@After
public void tearDown() throws IOException{
session.close();
buffer.close();
System.out.println("TEARDOWN!");
}
}
我的 input.txt 也将如下所示:
2 true
3 true
-1 false