Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

java - OpenCSV doesn't write headers in case of empty bean list

When using opencsv with annotation and StatefulBeanToCsv, headers are not written when bean list is empty. I would assume this test successful but it's not.

private class bean1 {
    @CsvBindByName(column = "column1")
    private String col1;

    public String getCol1() {
        return col1;
    }

    public void setCol1(String col1) {
        this.col1 = col1;
    }
}

@Test
public void testOpenCsvEmptyBeanList() throws CsvException {
    StringWriter sw = new StringWriter();
    
    StatefulBeanToCsv<bean1> sbc = new StatefulBeanToCsvBuilder<bean1>(sw)
            .withLineEnd(CSVWriter.DEFAULT_LINE_END)
            .build();
    
    // empty bean list
    sbc.write(new ArrayList<>());
    
    assertEquals("column1" + CSVWriter.DEFAULT_LINE_END, sw.toString());
}

Any idea to make this test works ?

question from:https://stackoverflow.com/questions/65882442/opencsv-doesnt-write-headers-in-case-of-empty-bean-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...