gpt4 book ai didi

java - 用于读取 csv 文件的 JUnit 测试

转载 作者:行者123 更新时间:2023-11-30 11:17:08 25 4
gpt4 key购买 nike

我有一种方法可以逐行读取 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;
}

最佳答案

您不能使用 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]);
}
}

关于java - 用于读取 csv 文件的 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24533208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com