Kotlinlearncs.online LogoJava
Return to List

Test Writing: Comparable Cat

Created By: Geoffrey Challen
/ Version: 2020.10.0

Create a class called Cat that implements the Comparable interface. Cat should provide a public constructor that takes a single double argument that sets that cat's age. You should assert that the provided age is not negative. You should order cats based on their age. Specifically, compareTo should return:

  • -1 if this cat is younger than the passed cat
  • 0 if this cat is the same age as the passed cat, or if the passed value is not a Cat
  • 1 if this cat is older than the passed cat

As a reminder, the Comparable interface comprises a single method: int compareTo(Object other).

Test Design Challenge

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

  • Provide a public class named TestCat 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 Java's assert method