/* Do these imports give you an error? JUnit may not be on your machine; to get it: 1) Download it (version 4 or higher; I've got 4.8.1) as a jar 2) In Eclipse go to the Project menu, then Properties, then Java Build Path, then Libraries, then Add External JARs, then locate the junit jar 3) To run it your test class in Eclipse, just 'Run' the class (such as by hitting F11) */ import static org.junit.Assert.*; //use 'static' import to import static methods of a class import org.junit.*; public class MyArrayTest { private MyArray arr; private final static int testSize=10000; //methods annotated with 'Before' will be run preceding each other method; preparation for the tests @Before public void init() { arr=new MyArray(); } //methods annotated with 'Test' are run one by one, and are considered // successful if they terminate normally, that is, unless one of these occurs: // 1: method throws an exception // 2: fail() gets called // 3: an assert() fails //hence, we need to catch (and ignore) exceptions we expect to occur @Test public void testEmptyGet() { try { arr.get(0); fail("get() on an empty array is invalid"); } catch(ArrayIndexOutOfBoundsException e){} } @Test public void testNegativeGet() { arr.add("first"); try { arr.get(-1); fail("get() with a negative index is invalid"); } catch(ArrayIndexOutOfBoundsException e){} } @Test public void testLotsOfAdds() { for(int i=0;i<1000;i++) arr.add("n"+i); } @Test public void testOutOfBounds() { for(int i=0;i<1000;i++) arr.add("n"+i); try { arr.get(arr.getSize()); fail("get() with an out of bounds index is invalid"); } catch(ArrayIndexOutOfBoundsException e){} } @Test public void testGetSize() { for(int i=0;i