Kotlinlearncs.online LogoJava
Return to List

Test Writing: String Name Occupation Parsing

Created By: Aisha Salau
/ Version: 2023.7.0

Write a method name parseNameOccupation that accepts a single non-null String in the following format:

  • "Aisha, a coder!"
  • "Felip, a doctor!"
  • "Gracie, a dog!"

Specifically, the input String has the following format: ", a !". (Gracie's one and only job is being a dog.) Note that the name will always be followed immediately by a comma, and the occupation by an exclamation point.

You should return a new String reformatted as follows: "My friend is a great !". So, given "Aisha, a coder!", you would return "My friend Aisha is a great coder!" Note that we have removed the comma but kept the exclamation point.

To complete this problem you will probably want to utilize the String methods split and substring.

Test Design Challenge

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

  • Provide a 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: