Kotlinlearncs.online LogoJava
Return to List

Test Writing: Flop Flip Object

Created By: Geoffrey Challen
/ Version: 2021.2.0

Define a public class named Flop with a single public instance method named flip that takes no parameters and returns a boolean. Flop should also provide a single public constructor that accepts a boolean argument and sets the initial state of the Flop instance.

Flop maintains one piece of private state: the boolean. Calling flip changes the boolean from true to false or false to true and returns the old (not the new) 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 TestFlop 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