Kotlinlearncs.online LogoJava
Return to List

Test Writing: Comparable Fish

Created By: Geoffrey Challen
/ Version: 2020.3.0

Create a class called Fish that implements the Comparable interface. Fish should provide a public constructor that takes a single double argument that sets that fish's length. You should assert that the provided length is greater than zero. You should order fish based on their length, but in reverse order. (Small fish rule!) Specifically, compareTo should return:

  • -1 if this fish is longer than the passed fish
  • 0 if this fish is the same length as the passed fish, or if the passed value is not a Fish
  • 1 if this fish is shorter than the passed fish

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 TestFish 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