Unit Testing Arrays And Array Lists
Unit Testing
The purpose of unit tests is to automatically run a set of tests every time to ensure your code continues to work as expected.
A popular unit testing framework in Java is JUnit.
A unit test is a set of tests that tests the units (methods) of a single class. A test case tests the response of exactly one unit (method) to a particular set of inputs.
Bonus: Integration testing tests how well classes and methods work together. This is outside the scope of this course though.
Assertions
The basic idea of unit tests is to compare expected results to actual results.
Call method to get actual results.
Make an assertion about what the expected result should be compared to the actual result.
If JUnit fails a test, an
AssertionErroris thrown, which JUnit catches and displays for you.Rinse and repeat as many times as necessary.
In JUnit, here's some common assert methods:
assertTrue(boolean test, String message)wheremessageis optionalassertFalse(boolean test, String message)wheremessageis optionalassertEqualsassertNotEqualsassertArrayEqualscompares contents of two arrays for equalityassertSamechecks for referential equalityassertNotSameassertNullandassertNotNullassertThrowsandassertDoesNotThrowchecks for an exception
Pro tip: We don't really test getter methods that just return internal fields in a class instance.
Creating a JUnit test
A JUnit test typically has two main parts:
A
@BeforeEachmethod that performssetUpbefore each testMultiple test methods each annotated with
@Test
Testing for equality
Recall that comparing Objects depends on the equals method, which needs to be custom implemented for custom Objects.
JUnit's assertEquals method uses == to compare primitives and the equals method to compare Objects. So, if you're comparing two custom Objects, make sure the equals method has been implemented.
Arrays
Arrays have a lot of the common features of ordered collections:
Indexes items from 0 to length of array minus 1
Can contain any type of element (primitives or Objects)
Some unique features though:
Can only store the same data type
Can only have a fixed number of slots
Because of this, arrays cannot be easily resized; you have to create a new array and copy over the contents of the old array
Initializing and updating an array
Here's the basic Java syntax for initializing and updating an array in two separate steps:
Note: When accessing indices outside of the range set by the array during initialization, Java will throw a runtime error.
You can, however, initialize and add to an array all in one step too:
Note: However, when pre-populating your array, you must do so during initialization. You cannot declare the variable and then add the array items afterwards.
Array fields and methods
To access the length of an array, arrays have a field available: arr.length.
Typical operations like adding, removing, or reversing an array are not available.
Here's an array's available methods: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Array.html.
Iterating through an array
Nested arrays
Note: To check if two nested arrays are equal, you can use Arrays.deepEquals.
ArrayLists
ArrayLists are just like arrays but more flexible; they work just like lists in Python.
Features:
Have a variable number of slots
Can only contain Objects of the same type
Part of Java's
CollectionsframeworkYou must explicitly import it via
import java.util.ArrayList
Initializing an ArrayList
ArrayListNote: For primitive data types, you can use them as long as you pass the wrapper class that makes the primitives into Objects--like Integer for int or Double for double.
ArrayList fields and methods
ArrayList fields and methodsCommon methods to use:
addremovegetsizeFull documentation: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
Last updated