Kotlinlearncs.online LogoJava
Return to List

Test Writing: OurComparable Car

Created By: Geoffrey Challen
/ Version: 2023.10.0

Create a class called Car that implements the OurComparable interface. Car should provide a primary constructor that takes a single Int argument that sets that car's odometer. You should assert that the provided Int is not negative. Do not expose the odometer property.

You should order cars based on their odometer reading, from least to greatest. Specifically, compareTo should return:

  • -1 if this car has a lower odometer reading than the passed car
  • 0 if this car has the same odometer reading as the passed car
  • 1 if this car has a higher odometer reading than the passed car

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

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

Test Design Challenge

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

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