Kotlinlearncs.online LogoJava
Return to List

Test Writing: OurComparable Dog

Created By: Geoffrey Challen
/ Version: 2023.10.0

Create a class called Dog that implements the OurComparable interface. Dog should provide a public constructor that takes a single Double argument that sets that dog's weight. You should assert that the provided weight is greater than zero. Do not expose the weight property.

You should order dogs based on their weight from lightest to heaviest. Specifically, compareTo should return:

  • -1 if this dog is lighter than the passed dog
  • 0 if this dog is the same weight as the passed dog
  • 1 if this dog is heavier than the passed dog

If the passed value is not a Dog, throw an IllegalArgumentException.

As a reminder, the OurComparable interface comprises a single method: fun compareTo(other: Any?): Int.

Test Design Challenge

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

  • Provide a public class named TestDog 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