首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Opencsv CSVReader中异常的测试

Opencsv CSVReader中异常的测试
EN

Stack Overflow用户
提问于 2022-11-12 18:45:46
回答 1查看 32关注 0票数 0

在使用Opencsv的CSVReader时,我正在尝试测试异常处理。数据将来自一个字符串。它不起作用,因为我(可能)没有正确地嘲弄CSVReader,但无法完全弄清楚我需要做什么。

这是一堂课

代码语言:javascript
复制
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvValidationException;
// other imports skipped

public class MyCsvReader {
    private Path contentsAsString;
    private CSVReader csvReader;

    public MyCsvReader(final String contentsAsString) {
        InputStream inputStream = new ByteArrayInputStream(contentsAsString.getBytes());
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);

        csvReader = new CSVReaderBuilder(inputStreamReader)
                .withSkipLines(0)
                .withKeepCarriageReturn(false)
                .build();
    }

    public void readData() {
        String[] line;

        try {
            while ((line = csvReader.readNext()) != null) {
                System.out.println("line:" + Arrays.toString(line));
            }
        } catch (IOException e) {
            System.out.println("got IOException");
            // I will be throwing a custom exception here
            throw new RuntimeException(e);
        } catch (CsvValidationException e) {
            System.out.println("got CsvValidationException");
            // and a different custom exception here
            throw new RuntimeException(e);
        }
    }
}

以及测试

代码语言:javascript
复制
public class MyCsvReaderTest {

    @Test
    public void testException() throws Exception {
        String[] rows = {
                "column1,column2,column3",
                "test,abc,def"
        };
        String rowData = String.join("\n", rows);

        CSVReader mock = Mockito.mock(CSVReader.class);
        Mockito.when(mock.readNext()).thenThrow(new IOException("test"));

        MyCsvReader reader = new MyCsvReader(rowData);
        try {
            reader.readData();
            fail("Expected an exception, but call succeeded");
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
}

当我运行它时,reader.readNext()不会引发异常

代码语言:javascript
复制
line: [column1, column2, column3]
line: [test, abc, def]

org.opentest4j.AssertionFailedError: Expected and exception, but call succeeded
... stack trace deleted

关于我需要做什么的建议?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-12 19:45:34

您使用的不是模拟的实例,而是真正的实例。如果这里的目标是在发生mock.readNext()时抛出一个异常,那么您必须这样做,以便注入您模拟的CSVReader实例

代码语言:javascript
复制
public class MyCsvReader {
    private final CSVReader csvReader;

    public MyCsvReader(final CSVReader csvReader) {
        this.csvReader = csvReader;
    }

    public MyCsvReader(final String contentsAsString) {
        this(getCsvReader(contentsAsString));
    }

    private static CSVReader getCsvReader(final String contentsAsString) {
        InputStream inputStream = new ByteArrayInputStream(contentsAsString.getBytes());
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        return new CSVReaderBuilder(inputStreamReader)
            .withSkipLines(0)
            .withKeepCarriageReturn(false)
            .build();
    }

    public void readData() {
        String[] line;

        try {
            while ((line = csvReader.readNext()) != null) {
                System.out.println("line:" + Arrays.toString(line));
            }
        } catch (IOException e) {
            System.out.println("got IOException");
            // I will be throwing a custom exception here
            throw new RuntimeException(e);
        } catch (CsvValidationException e) {
            System.out.println("got CsvValidationException");
            // and a different custom exception here
            throw new RuntimeException(e);
        }
    }
}
代码语言:javascript
复制
public class MyCsvReaderTest {
    @Test
    public void testException() throws Exception {
        CSVReader mock = Mockito.mock(CSVReader.class);
        Mockito.when(mock.readNext()).thenThrow(new IOException("test"));

        MyCsvReader reader = new MyCsvReader(mock);
        try {
            reader.readData();
            fail("Expected an exception, but call succeeded");
        } catch (RuntimeException ignored) {
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74415631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档