testing - Test driven development: Void methods -


I'm learning about TDD (using JUnit) and I have doubts about how to get rid of zero methods To be tested, in which case I can not use any type of value directly for any method (for example). For example, I have a simple console based application, and a part of it prints a menu on the screen, using this method:

  Public Zero Print Menu () { System.out.println ("menu:"); System.out.println ("1. Option 1"); System.out.println ("2. Option Two"); System.out.println ("Exit 3."); }  

My question is, do I really have to test this method ?? And if so, how should I do this?

First of all: The test UI is difficult. Some people do not bother to test things in this way because it is very difficult to write a meaningful test which is not fragile for the unemployed situation. I would not have been bothered to test this method.

But:

If you want to test the menu generation, because your menu code is complex and you need ways to make sure that it works, There are options.

  1. Reflect your method so that it can accept the output stream as a parameter, then pass it into an output stream, whose content you can observe. You may also be able to redirect System.out to get it.
  2. Refact your method so that the menu is prepared as a bunch of objects, then print it separately. You can inspect and verify those items.

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -