Kotlinlearncs.online LogoJava
Return to List

Test Writing: Word Lengths With Map

Created By: Geoffrey Challen
/ Version: 2021.9.0

Given a String? containing words separated by the " " (space) character, write a method wordLengths that return a Map<String, Int> mapping each word that is contained in the String to its length. require that the passed String? is not null.

So, for example, given the String "Wow that is amazing", you would return a map with four mappings: "Wow" to 3, "that" to 4, "is" to 2, and "amazing" to 7.

Note that you should not need any import statements, since Lists are built-in to Kotlin and always available.

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 Kotlin's assert or check methods