Kotlinlearncs.online LogoJava
Return to List

Test Writing: Polymorphic Greeter

Created By: Geoffrey Challen
/ Version: 2020.9.0

Write a method name greet that accepts a nullable Person as an argument and returns a String or null.

Depending on what kind of person it is, you should greet them differently:

  • If the person is a Professor—for instance, one named "Geoff"—you should greet them "Hi Professor Geoff"
  • If the person is a Student—for instance, one named "Friendly"—you should greet them "Hey Friendly, you are not alone!"
  • If the person is a Staff, then they will have a String role you can retrieve using getRole. For example, if their role is "advising" their name is "Chuchu", you should greet them "Thanks Chuchu for all your help with advising".

All Persons have a name property. If the person is null or not one of the kinds of people described above, return null. Do not solve this problem using method overloading. And do not hard-code the answers. Your solution should work for any Professor, Student, or Staff.

Test Design Challenge

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

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