Kotlinlearncs.online LogoJava
Return to List

Test Writing: SimpleArrayList add

Created By: Geoffrey Challen
/ Version: 2020.10.0

Let's write the add method for our SimpleArrayList. First, create a SimpleArrayList class with a single public constructor that initializes the list with a passed non-null array of Any? references. Call the array property values, and it should be publicly readable but not publicly writable. Also provide a method size() with that returns the current size of the list.

Now write the add method, which takes the position to add at as an Int as its first parameter and the Any? reference to add as its second. add should add the element to the list, increasing the size by one and shifting elements after the add position backward. You should assert that the passed position is valid for this list. But note that you should allow adding a new item to the end of the existing list.

When you are done, here is how your SimpleArrayList class should work:

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a public class named TestSimpleArrayList with a single non-private class method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Kotlin's assert or check methods