Lab 3 – MyArrayList
Lab 3 – MyArrayList
- Lab 3 Home
- Warmup
- Part 2
- Part 2
- Part 3
- Submission
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, 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-parameteradd()
method), and theget()
method. Under the covers, it will also test your privateresize()
method. This also tests adds to the end of the list and adds when the list is at capacity.Create a
MyArrayList
ofIntegers
, and then insert the numbers 0 through 20 using theadd(element)
method. Then loop through the list using theget()
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
ofIntegers
, and insert the numbers 0 through 20, but insert each one at the front of the list usingadd(index, element)
. Iterate through your list and use theget()
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
ofIntegers
and insert the numbers 0 through 20, but this time insert each number at the midpointsize() / 2
of the list. This should result in the ordering 1, 3, 5, 7, 9, 10, 8, 6, 4, 2, 0. Use theget()
method to test that your inserts are ordered appropriately. void testAddOutOfBounds()
andvoid testGetOutOfBounds()
- Write methods to test the
IndexOutOfBoundsException
s on both borders (index-1
and indexsize()
orsize() + 1
, depending on the method) for the parameterizedadd()
method and theget()
method.
As you implement the rest of your MyArrayList
methods, please write the
accompanying JUnit test, and add to any previous ones.