Unit Tests
- Write simple unit tests using the JUnit framework.
Let's add a unit test for the length
method.
@Test
void length() {
IndexedList<Integer> numbers = new ArrayIndexedList<>(5, 10);
assertEquals(5, numbers.length());
}
In the test above, I've created an IndexedList with the size of 5 and the default value of 10. Then, I've asserted the length()
method returns the value 5, the size which was provided earlier.
Notes:
-
IndexedList
cannot be instantiated (because it is an interface) so we must use a concrete implementation of it (e.g.ArrayIndexedList
). Therefore, the test is actually testing an implementation oflength
. -
assertEquals
is one of the many utility methods provided by JUnit. It ensures its two arguments are equal. If not, it will signal to "fail" the test. To useassertEquals
you must import it:import static org.junit.jupiter.api.Assertions.assertEquals;