Java 如何设计数据驱动的JUnit测试类 @参数 公共静态集合数据()引发IOException{ ArrayList行=新的ArrayList(); URL URL=PokerhandTestCase.class.getClassLoader().getResource(“test/TestFile.txt”); File testFile=新文件(url.getFile()); FileReader FileReader=新的FileReader(testFile); bufReader=新的BufferedReader(文件阅读器); assertFalse(“加载测试文件失败。”,testFile==null); 布尔isEOF=假; 而(!isEOF){ 字符串aline=bufReader.readLine(); if(aline==null){ System.out.println(“完成处理”); isEOF=真; } 行。添加(aline); } 返回数组.asList(行); }
程序的最后一行导致了崩溃,我想知道从arrayList定义集合的正确方法是什么。此函数用于将集合作为返回类型。将最后一行替换为:Java 如何设计数据驱动的JUnit测试类 @参数 公共静态集合数据()引发IOException{ ArrayList行=新的ArrayList(); URL URL=PokerhandTestCase.class.getClassLoader().getResource(“test/TestFile.txt”); File testFile=新文件(url.getFile()); FileReader FileReader=新的FileReader(testFile); bufReader=新的BufferedReader(文件阅读器); assertFalse(“加载测试文件失败。”,testFile==null); 布尔isEOF=假; 而(!isEOF){ 字符串aline=bufReader.readLine(); if(aline==null){ System.out.println(“完成处理”); isEOF=真; } 行。添加(aline); } 返回数组.asList(行); },java,junit,parameterized,data-driven-tests,Java,Junit,Parameterized,Data Driven Tests,程序的最后一行导致了崩溃,我想知道从arrayList定义集合的正确方法是什么。此函数用于将集合作为返回类型。将最后一行替换为: @Parameters public static Collection data() throws IOException { ArrayList<String> lines = new ArrayList(); URL url = PokerhandTestCase.class.getClassLoader().getRes
@Parameters
public static Collection data() throws IOException {
ArrayList<String> lines = new ArrayList();
URL url = PokerhandTestCase.class.getClassLoader().getResource("test/TestFile.txt");
File testFile = new File(url.getFile());
FileReader fileReader = new FileReader(testFile);
bufReader = new BufferedReader(fileReader);
assertFalse("Failed to load the test file.", testFile == null);
boolean isEOF = false;
while (!isEOF){
String aline = bufReader.readLine();
if (aline == null){
System.out.println("Done processing.");
isEOF = true;
}
lines.add(aline);
}
return Arrays.asList(lines);
}
由于ArrayList实现了集合接口:
因此,总体代码:
return (Collection)lines;
public static Collection data()引发IOException
{
ArrayList行=新的ArrayList();
//填充行集合。。。
退货(收款)行;
}
根据下面的评论,这可能符合“数组集合”的条件:
public static Collection data()引发IOException
{
ArrayList array1=新的ArrayList();
ArrayList array2=新的ArrayList();
ArrayList array3=新的ArrayList();
//填充行集合。。。
ArrayList行=新的ArrayList();
行。添加(数组1);
行。添加(array2);
行。添加(数组3);
退货(收款)行;
}
ArrayList行=新的ArrayList();
...
返回数组.asList(行)
这个返回是二维数组
此函数需要将集合作为返回类型
我认为user1697575的回答是正确的。您要返回的收藏需要是
收藏。您正在返回一个集合。您需要这样做(作为一个完整的示例):
@RunWith(参数化的.class)
公共类MyTest{
@参数
公共静态集合数据()引发IOException{
列表行=新的ArrayList();
File testFile=新文件(“/temp/testFile.txt”);
FileReader FileReader=新的FileReader(testFile);
BufferedReader bufReader=新的BufferedReader(文件读取器);
Assert.assertFalse(“加载测试文件失败。”,testFile==null);
布尔isEOF=假;
而(!isEOF){
字符串aline=bufReader.readLine();
if(aline==null){
System.out.println(“完成处理”);
isEOF=真;
}
添加(新对象[]{aline});
}
回流线;
}
私有最终字符串文件;
公共MyTest(字符串文件){
this.file=文件;
}
@试验
公开无效测试(){
System.out.println(“file=“+file”);
}
}
请注意,您没有关闭此处的文件,并且在列表末尾添加了一个无用的空值,但我复制了您的代码:-)。编译器抱怨此函数必须返回数组集合。@Alfred我不这么认为。也许您的代码中有一个输入错误。它的可编译代码。提供编译器错误plz.Sry,我不是说这是一个编译错误。但是JUnit框架定义此函数应返回数组集合。我想设计一个数据驱动的junit测试类,所以我创建了一个文本文件,并希望逐行读取,然后作为字符串数组返回。但是它需要一个数组集合,所以我想也许我应该让每个数组包含一个字符串对象。感谢lotjava.lang.Exception:src.PokerhandTestCase.data()必须返回数组集合。我尝试了前面的答案,但JUnit不接受ArrayList of ArrayList。它只接受类型Object[][]atype=new Object[]{{1},{2},{3}。我想定义一个arraylist,以便收集从testfile.txt读取的字符串。上述声明是静态定义的。
public static Collection data() throws IOException
{
ArrayList<String> lines = new ArrayList();
// populate lines collection...
return (Collection)lines;
}
public static Collection data() throws IOException
{
ArrayList<String> array1 = new ArrayList();
ArrayList<String> array2 = new ArrayList();
ArrayList<String> array3 = new ArrayList();
// populate lines collection...
ArrayList<ArrayList<String>> lines = new ArrayList();
lines.add(array1);
lines.add(array2);
lines.add(array3);
return (Collection)lines;
}
@RunWith(Parameterized.class)
public class MyTest {
@Parameters
public static Collection<Object[]> data() throws IOException {
List<Object[]> lines = new ArrayList<>();
File testFile = new File("/temp/TestFile.txt");
FileReader fileReader = new FileReader(testFile);
BufferedReader bufReader = new BufferedReader(fileReader);
Assert.assertFalse("Failed to load the test file.", testFile == null);
boolean isEOF = false;
while (!isEOF) {
String aline = bufReader.readLine();
if (aline == null) {
System.out.println("Done processing.");
isEOF = true;
}
lines.add(new Object[] { aline });
}
return lines;
}
private final String file;
public MyTest(String file) {
this.file = file;
}
@Test
public void test() {
System.out.println("file=" + file);
}
}