Kotlinlearncs.online LogoJava
Return to List

Test Writing: Remove Empty and Null from List (with assert)

Created By: Justin Maier
/ Version: 2024.11.0

Write a void method named cleanList that takes a List<String> as an argument and modifies the list to remove all elements that are either the empty String ("") or null.

Remember that you can use remove to remove elements from a List, though you should be careful that you do not remove elements from a List while you are iterating over it (with a for loop, for example).

You should assert that the List argument is not null.

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
void cleanList(List<String> list) {
return; // You may need to remove this starter code
}