Kotlinlearncs.online LogoJava
Return to List

Test Writing: Even Counter

Created By: Geoffrey Challen
/ Version: 2021.2.0

Define a public class named EvenCounter. It should have a public instance method named addValue taking a single int argument. If the argument passed to addValue is even, it increments the count stored by the counter. addValue does not return a value. You should provide a getter for the count getCount. Your class should also provide a constructor taking a single int argument that sets the initial count, which you should assert is non-negative. Your class should not allow modifications to the count except using the addValue method.

When you are finished your class should work as follows:

Test Design Challenge

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

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