【问题标题】:Testing for exceptions in Opencsv CSVReader在 Opencsv CSVReader 中测试异常
【发布时间】:2022-11-13 04:09:12
【问题描述】:

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

这是课程

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);
        }
    }
}

和测试

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() 不会抛出异常

line: [column1, column2, column3]
line: [test, abc, def]

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

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

【问题讨论】:

    标签: java mockito opencsv


    【解决方案1】:

    您不使用模拟实例,而是使用真实实例。 如果这里的目标是在 mock.readNext() 发生时抛出异常,则您必须执行类似的操作才能注入 CSVReader 的模拟实例:

    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);
            }
        }
    }
    
    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) {
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 2018-12-31
      • 2019-06-13
      相关资源
      最近更新 更多