我有一个逐行读取csv文件的方法,并将每个记录放在一个列表中。我想测试一下这个方法。我所做的是创建了一个预期记录列表,然后我创建了一个csv文件,其中包含我在列表中创建的相同记录。我读了这个新创建的csv文件,并把它放在实际的记录列表中。然后我比较两个列表,但它给了我一个断言错误。
java.lang.AssertionError:expected:< [[Ljava.lang.String; @ 91b9b0]>但是< [[Ljava.lang.String; @ 1360c93]>
这是我的代码
String[] record = new String[] { "source", "name", "component", "10", "100", "10.5", "200", "15.5" };
MyReader reader = new MyReader();
@Test
public void readDataTest () throws FileNotFoundException
{
List< String[] > expectedrecords = new ArrayList< String[] >();
expectedrecords.add( record );
List< String[] > actualrecords = new ArrayList< String[] >();
generatetestCSV( dirPath + "\\Messages_Statistics.csv" );
actualrecords = reader.readData( dirPath + "\\Messages_Statistics.csv" );
Assert.assertEquals( expectedrecords, actualrecords );
}
private void generatetestCSV ( String fileName )
{
try
{
FileWriter writer = new FileWriter( fileName );
writer.append( "source" );
writer.append( ',' );
writer.append( "name" );
writer.append( ',' );
writer.append( "component" );
writer.append( ',' );
writer.append( "10" );
writer.append( ',' );
writer.append( "100" );
writer.append( ',' );
writer.append( "10.5" );
writer.append( ',' );
writer.append( "200" );
writer.append( ',' );
writer.append( "15.5" );
writer.append( '\n' );
writer.flush();
writer.close();
}
catch ( IOException e )
{
e.printStackTrace();
}
}
MyReader类中的readData是
public List< String[] > readData ( String csvFile ) throws FileNotFoundException
{
List< String[] > records = new ArrayList< String[] >();
BufferedReader br = new BufferedReader( new FileReader( csvFile ) );
String line = "";
try
{
if ( csvFile != null )
{
if ( csvFile.contains( "Messages_Statistics" ) )
{
while ( ( line = br.readLine() ) != null )
{
String[] record = line.split( "," );
if ( records.size() < LIMIT )
records.add( record );
else
break;
}
}
}
}
catch ( FileNotFoundException e )
{
//logger.error( "Error in reading CSV file", e );
}
catch ( IOException e )
{
e.printStackTrace();
}
finally
{
if ( br != null )
{
try
{
br.close();
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
return records;
}
答案 0 :(得分:1)
您无法使用Assert.assertEquals
来比较List<String[]>
。这不起作用:
List<String[]> expected = new ArrayList<>();
expected.add(new String[]{"foo", "bar"});
expected.add(new String[]{"baz", "qux"});
List<String[]> actual = new ArrayList<>();
actual.add(new String[]{"foo", "bar"});
actual.add(new String[]{"baz", "qux"});
Assert.assertEquals(expected, actual); // Fails!
你必须这样做:
List<String[]> expected = new ArrayList<>();
expected.add(new String[]{"foo", "bar"});
expected.add(new String[]{"baz", "qux"});
List<String[]> actual = new ArrayList<>();
actual.add(new String[]{"foo", "bar"});
actual.add(new String[]{"baz", "qux"});
assertEquals(expected.size(), actual.size());
for (int i = 0; i < expected.size(); i++) {
for (int j = 0; j < expected.get(i).length; j++) {
Assert.assertEquals(expected.get(i)[j], actual.get(i)[j]);
}
}
答案 1 :(得分:0)
Assert.assertEquals( expectedrecords, actualrecords );
这是比较不相同的列表对象实例。您需要遍历循环并对列表中的各个条目进行断言
更改Assert检查,如下所示
Assert.assertEquals(expectedrecords.size(),actualrecords.size());
int count = 0;
for(String[] lineData: expectedrecords)
{
String[] actualRecordData = actualrecords.get(count);
count++;
Assert.assertEquals( lineData.length,actualRecordData.length);
for(int i = 0; i < lineData.length; i++)
{
Assert.assertEquals( lineData[i],actualRecordData[i]);
}
}
答案 2 :(得分:0)
编写一个将列表格式化为字符串的函数,然后比较字符串。如果他们匹配,那你很好。如果不这样做,则字符串diff比描述列表中的索引的异常更具可读性。特别是如果你在IntelliJ或Eclipse中运行测试,他们在那里格式化字符串以使差异非常明显。