Methods with unexpected return values (null or exceptions) have been
documented. All other methods will just fail as if the record came from
a parser without a header.
* CSV-252: Clean up assertions using assertThrows
As a followup to commit e2f0a4d8a8 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
* CSV-252 Stop using junit.framework.TestCase
junit.framework.TestCase is a class from JUnit 3, and while it is not
officially deprecated, it's discouraged to use it.
This patch removes the single use of
junit.framework.TestCase#assertNull, and replaces it with the
standard, recommended, org.junit.Assert#assertNull.
* CSV-252 Standardize org.junit.Assert imports
Code in the project uses org.junit.Assert's methods in two ways:
1. By statically importing them
2. By importing the class and using its methods
Option 1 seems to be the de-facto standard, with just a handful of
cases using Option 2.
This patch standardizes these cases to also use static imports thus
making the code look more uniform, and easier to maintain.
* CSV-252 Upgrade Mockito to 3.1.0
Upgrade the Mockito dependency to the latest available version, 3.1.0,
in order to facilitate an upgrade to JUnit Jupiter.
* CSV-252 JUnit Jupiter upgrade
This patch upgrades the project's testing framework from JUnit 4.12
to the modern JUnit Jupiter 5.5.4.
Since JUnit 5 Jupiter is not backwards compatible to JUnit 4.x (or
even JUnit Vintage), this patch is a bit large, even though a lot of
the changes are merely cosmetic (such as changing the argument order,
see details below). In order to make the reviewer's task as easy as
possible, this PR does not presume to use JUnit Jupiter's best
practices and all its new functionality, but only to migrate the
existing tests with as little change as possible. Following patches
may want to improve the tests by using some of JUnit Jupiter's new
features.
This patch includes the following changes:
1. Maven dependency changes:
a. junit:junit was replaced with org.junit.jupiter:junit-jupiter.
b. org.hamcrest:hamcrest was introduced as an explicit dependency,
since the project uses Hamcrest, and JUnit Jupiter does not
bundle Hamcrest, unlike JUnit 4.x.
2. Annotations:
a. org.junit.jupiter.api.Test was used as a drop in replacement for
org.juit.Test without arguments. See 3.ii. for handling of @Test
annotations with an "expected" argument.
b. org.junit.jupiter.api.BeforeEach was used as an drop in
replacement for org.junit.Before.
c. org.junit.jupiter.api.BeforeAll was used as an drop in
replacement for org.junit.BeforeClass.
d. org.junit.jupiter.api.Disabled was used as a drop in replacement
for org.junit.Ignore.
3. Assertions:
a. org.junit.jupiter.api.Assertions' methods were used as drop in
replacements for org.junit.Assert's methods with the same name in
the simple case of an assertion without a message. In the case of
an assertion with a message, org.junit.jupiter.api.Assertions'
methods were used, but the argument order was changed - Assert's
methods take the message as the first argument, while Assertions'
methods take the message as the last argument.
b. org.junit.jupiter.api.Assertions#assertThrows was used to assert
that a specific exception was throws instead of an org.junit.Test
annotation with an expected argument. This technique has a couple
of side bonuses. First, it makes the tests slightly stricter, as
now they can assert the exception was thrown from a specific line
and prevent false positives where the test's "set-up" code
accidentally threw that exception. Second, it clarifies that some
of the test code is unreachable (as a previous line already
throws an exception), and can safely be removed in order to clean
up the test. The throws clauses of these methods were cleaned up
from exceptions that can no longer be thrown in order to avoid
compilation warnings.
c. org.hamcrest.MatcherAssert#assertThat was used as a drop in
replacement for org.junit.Assert#assertThat.
4. Specific Changes:
a. CSVFileParserTest was rewritten with JUnit Jupiter's
org.junit.jupiter.api.ParameterizedTest. Unlike JUnit 4's
org.junit.runners.Parameterized, it cannot be used to inject
arguments to a test's construct, and so the test can't be
stateful. Instead, it was rewritten so every test receives the
file as a parameter, and opens a reader on it itself. As a side
bonus, this design makes it easier to close the reader and avoid
leaving open file descriptors like the original test did.