Kotlinlearncs.online LogoJava
Return to List

Test Writing: SimpleArrayList get and set

Created By: Geoffrey Challen
/ Version: 2020.10.0

Let's begin building a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList with a public constructor that initializes the list using a passed non-nullable array of Any? references. Your array should be private.

Next, implement:

  1. fun get(Int): Any, which takes an Int index and returns the Any at that index
  2. fun set(Int, Any?), which takes an Int index and an Any reference and sets that value at the index to the passed reference.

Both your get and set method should require that the index passed is valid for that SimpleArrayList. Here's an example of how your SimpleArrayList should work:

Don't overthink this! Both get and set should be two lines of code (including one for the require).

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