Skip to content

Commit 485929e

Browse files
mureinikgarydgregory
authored andcommitted
CSV-252: Clean up exception handling (apache#50)
* CSV-252: Clean up assertions using assertThrows As a followup to commit e2f0a4d that introduced JUnit Jupiter to the project, this patch leverages the new Assertions#assertThrows method to clean up tests for expected exceptions. Instead of the somewhat clunky structure common in JUnit 4 tests: ``` try { someMethod(); fail("SomeException should be thrown"); } catch (SomeException e) { // Expected... // Possibly some assertion on e } ``` JUnit Jupiter allows the following elegant syntax: ``` SomeException e = assertThrows(SomeException.class, () -> someMethod()); // Possibly some assertions on e ``` * CSV-252: Remove redundant throws clauses from tests
1 parent 0300569 commit 485929e

4 files changed

Lines changed: 24 additions & 45 deletions

File tree

src/test/java/org/apache/commons/csv/AssertionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class AssertionsTest {
2727

2828
@Test
29-
public void testNotNull() throws Exception {
29+
public void testNotNull() {
3030
Assertions.notNull(new Object(), "object");
3131
}
3232

src/test/java/org/apache/commons/csv/CSVBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void init() throws IOException {
6767
in.close();
6868
}
6969

70-
private BufferedReader getReader() throws IOException {
70+
private BufferedReader getReader() {
7171
return new BufferedReader(new StringReader(data));
7272
}
7373

src/test/java/org/apache/commons/csv/CSVFormatTest.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -550,17 +550,12 @@ public void testFormatThrowsNullPointerException() {
550550

551551
final CSVFormat csvFormat = CSVFormat.MYSQL;
552552

553-
try {
554-
csvFormat.format((Object[]) null);
555-
fail("Expecting exception: NullPointerException");
556-
} catch(final NullPointerException e) {
557-
assertEquals(CSVFormat.class.getName(), e.getStackTrace()[0].getClassName());
558-
}
559-
553+
NullPointerException e = assertThrows(NullPointerException.class, () -> csvFormat.format((Object[]) null));
554+
assertEquals(CSVFormat.class.getName(), e.getStackTrace()[0].getClassName());
560555
}
561556

562557
@Test
563-
public void testGetHeader() throws Exception {
558+
public void testGetHeader() {
564559
final String[] header = new String[]{"one", "two", "three"};
565560
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
566561
// getHeader() makes a copy of the header array.
@@ -893,7 +888,7 @@ public void testToStringAndWithCommentMarkerTakingCharacter() {
893888
}
894889

895890
@Test
896-
public void testWithCommentStart() throws Exception {
891+
public void testWithCommentStart() {
897892
final CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentMarker('#');
898893
assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentMarker());
899894
}
@@ -904,7 +899,7 @@ public void testWithCommentStartCRThrowsException() {
904899
}
905900

906901
@Test
907-
public void testWithDelimiter() throws Exception {
902+
public void testWithDelimiter() {
908903
final CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!');
909904
assertEquals('!', formatWithDelimiter.getDelimiter());
910905
}
@@ -915,13 +910,13 @@ public void testWithDelimiterLFThrowsException() {
915910
}
916911

917912
@Test
918-
public void testWithEmptyEnum() throws Exception {
913+
public void testWithEmptyEnum() {
919914
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(EmptyEnum.class);
920915
assertTrue(formatWithHeader.getHeader().length == 0);
921916
}
922917

923918
@Test
924-
public void testWithEscape() throws Exception {
919+
public void testWithEscape() {
925920
final CSVFormat formatWithEscape = CSVFormat.DEFAULT.withEscape('&');
926921
assertEquals(Character.valueOf('&'), formatWithEscape.getEscapeCharacter());
927922
}
@@ -932,14 +927,14 @@ public void testWithEscapeCRThrowsExceptions() {
932927
}
933928

934929
@Test
935-
public void testWithFirstRecordAsHeader() throws Exception {
930+
public void testWithFirstRecordAsHeader() {
936931
final CSVFormat formatWithFirstRecordAsHeader = CSVFormat.DEFAULT.withFirstRecordAsHeader();
937932
assertTrue(formatWithFirstRecordAsHeader.getSkipHeaderRecord());
938933
assertTrue(formatWithFirstRecordAsHeader.getHeader().length == 0);
939934
}
940935

941936
@Test
942-
public void testWithHeader() throws Exception {
937+
public void testWithHeader() {
943938
final String[] header = new String[]{"one", "two", "three"};
944939
// withHeader() makes a copy of the header array.
945940
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
@@ -1111,35 +1106,35 @@ public void testWithHeaderComments() {
11111106

11121107

11131108
@Test
1114-
public void testWithHeaderEnum() throws Exception {
1109+
public void testWithHeaderEnum() {
11151110
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(Header.class);
11161111
assertArrayEquals(new String[]{ "Name", "Email", "Phone" }, formatWithHeader.getHeader());
11171112
}
11181113

11191114

11201115
@Test
1121-
public void testWithIgnoreEmptyLines() throws Exception {
1116+
public void testWithIgnoreEmptyLines() {
11221117
assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
11231118
assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines().getIgnoreEmptyLines());
11241119
}
11251120

11261121

11271122
@Test
1128-
public void testWithIgnoreSurround() throws Exception {
1123+
public void testWithIgnoreSurround() {
11291124
assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
11301125
assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces().getIgnoreSurroundingSpaces());
11311126
}
11321127

11331128

11341129
@Test
1135-
public void testWithNullString() throws Exception {
1130+
public void testWithNullString() {
11361131
final CSVFormat formatWithNullString = CSVFormat.DEFAULT.withNullString("null");
11371132
assertEquals("null", formatWithNullString.getNullString());
11381133
}
11391134

11401135

11411136
@Test
1142-
public void testWithQuoteChar() throws Exception {
1137+
public void testWithQuoteChar() {
11431138
final CSVFormat formatWithQuoteChar = CSVFormat.DEFAULT.withQuote('"');
11441139
assertEquals(Character.valueOf('"'), formatWithQuoteChar.getQuoteCharacter());
11451140
}
@@ -1151,31 +1146,31 @@ public void testWithQuoteLFThrowsException() {
11511146
}
11521147

11531148
@Test
1154-
public void testWithQuotePolicy() throws Exception {
1149+
public void testWithQuotePolicy() {
11551150
final CSVFormat formatWithQuotePolicy = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL);
11561151
assertEquals(QuoteMode.ALL, formatWithQuotePolicy.getQuoteMode());
11571152
}
11581153

11591154
@Test
1160-
public void testWithRecordSeparatorCR() throws Exception {
1155+
public void testWithRecordSeparatorCR() {
11611156
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CR);
11621157
assertEquals(String.valueOf(CR), formatWithRecordSeparator.getRecordSeparator());
11631158
}
11641159

11651160
@Test
1166-
public void testWithRecordSeparatorCRLF() throws Exception {
1161+
public void testWithRecordSeparatorCRLF() {
11671162
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CRLF);
11681163
assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator());
11691164
}
11701165

11711166
@Test
1172-
public void testWithRecordSeparatorLF() throws Exception {
1167+
public void testWithRecordSeparatorLF() {
11731168
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(LF);
11741169
assertEquals(String.valueOf(LF), formatWithRecordSeparator.getRecordSeparator());
11751170
}
11761171

11771172
@Test
1178-
public void testWithSystemRecordSeparator() throws Exception {
1173+
public void testWithSystemRecordSeparator() {
11791174
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withSystemRecordSeparator();
11801175
assertEquals(System.getProperty("line.separator"), formatWithRecordSeparator.getRecordSeparator());
11811176
}

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static org.junit.jupiter.api.Assertions.assertNull;
2828
import static org.junit.jupiter.api.Assertions.assertThrows;
2929
import static org.junit.jupiter.api.Assertions.assertTrue;
30-
import static org.junit.jupiter.api.Assertions.fail;
3130

3231
import java.io.File;
3332
import java.io.IOException;
@@ -529,12 +528,7 @@ public void testGetHeaderNamesReadOnly() throws IOException {
529528
CSVFormat.DEFAULT.withHeader("A", "B", "C"))) {
530529
final List<String> headerNames = parser.getHeaderNames();
531530
assertNotNull(headerNames);
532-
try {
533-
headerNames.add("This is a read-only list.");
534-
fail();
535-
} catch (final UnsupportedOperationException e) {
536-
// Yes.
537-
}
531+
assertThrows(UnsupportedOperationException.class, () -> headerNames.add("This is a read-only list."));
538532
}
539533
}
540534

@@ -758,12 +752,7 @@ public void testIterator() throws Exception {
758752
final Iterator<CSVRecord> iterator = CSVFormat.DEFAULT.parse(in).iterator();
759753

760754
assertTrue(iterator.hasNext());
761-
try {
762-
iterator.remove();
763-
fail("expected UnsupportedOperationException");
764-
} catch (final UnsupportedOperationException expected) {
765-
// expected
766-
}
755+
assertThrows(UnsupportedOperationException.class, iterator::remove);
767756
assertArrayEquals(new String[] { "a", "b", "c" }, iterator.next().values());
768757
assertArrayEquals(new String[] { "1", "2", "3" }, iterator.next().values());
769758
assertTrue(iterator.hasNext());
@@ -772,12 +761,7 @@ public void testIterator() throws Exception {
772761
assertArrayEquals(new String[] { "x", "y", "z" }, iterator.next().values());
773762
assertFalse(iterator.hasNext());
774763

775-
try {
776-
iterator.next();
777-
fail("NoSuchElementException expected");
778-
} catch (final NoSuchElementException e) {
779-
// expected
780-
}
764+
assertThrows(NoSuchElementException.class, iterator::next);
781765
}
782766

783767
@Test

0 commit comments

Comments
 (0)