I have been learning about TDD (using JUnit) and I have a doubt about how to go about testing void methods, in which case I can’t directly use something like an assertTrue() on the return value of a method.. For example, say I have a simple console based application, and a part of it prints a menu on screen, say using this method:
public void printMenu()
{
System.out.println("Menu:");
System.out.println("1. Option ONE");
System.out.println("2. Option TWO");
System.out.println("3. Exit");
}
My question is, do I actually have to test this method?? And if so, how should I do it?
First: testing UI is hard. Some people don’t bother testing things like this because it is very difficult to write meaningful tests that aren’t fragile to the point of uselessness. I wouldn’t bother testing this method.
But:
If you want to test menu generation, because your menu code is complex and you need ways to ensure that it works, you have a couple choices.