Kotlinlearncs.online LogoJava
Return to List

Test Writing: Book Class

Created By: Justin Maier
/ Version: 2024.9.0

Write a public class Book that has the following instance variables:

  • title: a String
  • author: a String
  • pages: an int representing the number of pages

You should supply two constructors: one that takes three arguments to initialize all three fields, and another that only takes two arguments and just initializes the title and author fields.

Additionally, you should supply getter methods getTitle, getAuthor, and getPages that return the respective fields.

You will need to prefix the class and constructors with public. We will talk about what this means next time!

Test Design Challenge

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

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

Related Lessons

Stuck? You may find these lessons helpful: