Kotlinlearncs.online LogoJava
Return to List

Test Writing: Toggler Object

Created By: Geoffrey Challen
/ Version: 2021.9.0

Define a class named Toggler with a single instance method named toggle that takes no parameters and returns a Boolean. Toggler should also provide a primary constructor that accepts a Boolean argument and sets the initial state of the Toggler instance.

Toggler maintains one piece of private state: the Boolean. Calling toggle changes the Boolean from true to false or false to true and returns the new (not the old) state of the Boolean. So, for example:

Note that the internal state should be private.

Test Design Challenge

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

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