在使用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关于我需要做什么的建议?谢谢!
发布于 2022-11-12 19:45:34
您使用的不是模拟的实例,而是真正的实例。如果这里的目标是在发生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) {
}
}
}https://stackoverflow.com/questions/74415631
复制相似问题