CS-151 Labs > Lab 3. My ArrayList


Part 2. Testing with JUnit

Now you will build JUnit tests for the methods you just wrote. First, create a set of JUnit tests for your class, adding the JUnit Test case file, like you did in the warmup.
Then, implement the following tests.

void testSize()
To test that your size method is working correctly, replace the fail(...) method call with the following code:
MyArrayList<Integer> test = new MyArrayList<Integer>(); 
assertEquals( "Size after construction", test.size(), 0);
test.add(0,5);
assertEquals( "Size after add", test.size(), 1);
void testAddE()
Tests your constructor, the single-parameter add() method (which, hopefully calls the two-parameter add() method), and the get() method. Under the covers, it will also test your private resize() method. Make sure to include test cases for adding to the end of the list and adding when the list is at capacity.

Create a MyArrayList of Integers, and then insert the numbers 0 through 20 using the add(element) method. Then loop through the list using the get() method to access each element of the list, and assert that the ith element is equal to i.

void testAddIntEFront()
Tests adds to the beginning of the list.

Create a MyArrayList of Integers, and insert the numbers 0 through 20, but insert each one at the front of the list using add(index, element). Iterate through your list and use the get() method to test that the ith element is equal to 20 - i.

void testAddIntEMid()
Tests adds to the middle of the list.

Again create a MyArrayList of Integers and insert the numbers 0 through 20, but this time insert each number at the midpoint size() / 2 of the list. This should result in the ordering 1, 3, 5, 7, 9, 10, 8, 6, 4, 2, 0. Use the get() method to test that your inserts are ordered appropriately.

void testAddOutOfBounds() and void testGetOutOfBounds()
Write methods to test the IndexOutOfBoundsExceptions on both borders (index -1 and index size() or size() + 1, depending on the method) for the parameterized add() method and the get() method.

As you implement the rest of your MyArrayList methods, please write the accompanying JUnit test, and add to the previous ones.