如何使用FileUtils从JUnit Test中的资源目录读取csv文件

时间:2015-11-12 07:50:02

标签: java apache-commons apache-commons-io apache-commons-csv

在标准maven项目的资源目录中有一个csv文件,如下所示:

src/main/resources/fruits.csv
src/test/resources/fruits.csv

fruits.csv

Type, Quantity
apple, 50
banana, 60
orange, 70

使用以下库

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.0</version>
</dependency>

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

水果(标准POJO)

public class Fruit {

    private String type
    private int quantity;

    public Fruit(String type, int quantity) {
        this.type = type;
        this.quantity = quantity;
    }

    // Getters & Setters
}

CsvFileReader

public class CsvFileReader {

    private static final String [] FILE_HEADER = {""};

    private static final String TYPE = "Type";
    private static final String QUANTITY + "Quantity";

    public static List<Candidate> readCsvFile(File fileName) {
        FileReader fileReader = null;
        CSVParser csvFileParser = null;

        CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER);
        List<Candidate> fruits = null;
        try {
            fruits = new ArrayList<>();
            String file = FileUtils.readFileToString(fileName);
            fileReader = new FileReader(file);
            csvFileParser = new CSVParser(fileReader, csvFormat);
            List<CSVRecord> records = csvFileParser.getRecords();

            for (int i = 1; i < records.size(); i++) {
                CSVRecord record = records.get(i);
                Fruit fruit = new Fruit(record.get(TYPE), Integer.parseInt(QUANTITY));
                fruits.add(fruit);
            }
        } 
        catch(Throwable t) {
            t.printStackTrace();
        } 
        finally {
            try {
                fileReader.close();
                csvFileParser.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
        return fruits;
    }
}

CsvFileReaderTest(JUnit 4测试用例):

public class CsvFileReaderTest {

    File csvFile = null;

    @Before
    public void setUp() throws Exception {
        String userDir = System.getProperty("user.dir");
        String filePath = userDir + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator;
        csvFile = FileUtils.getFile(filePath + "fruits.csv");
    }
}

当我在Eclipse中运行JUnit测试用例时:

java.io.FileNotFoundException: Type, Quantity
apple, 50
banana, 60
orange, 70
(File name too long)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at java.io.FileReader.<init>(FileReader.java:58)

它似乎在读取文件,但问题是FileName太长了?

我做错了什么?

1 个答案:

答案 0 :(得分:0)

FileReader构造函数需要一个文件路径,您可以在其中传递文件的内容。您可以直接使用带有CSV CSVParser的{​​{1}}构造函数。

String

**编辑**

更好的方法是获取资源的读者并将其传递给CSVParser构造函数:

csvFileParser = new CSVParser(file /*This is already file content*/, csvFormat);