The Daily WTF
We recently discussed the challenges of testing legacy code. Willy sent us some code from tests for a newer Java application.
Now, one of the main advantages, in my opinion, about unit-tests is that they serve as documentation. They tell you what the method does, and how it expects to be called. A good unit test is far better than a large pile of comments.
A bad unit test (or function supporting a unit test, as in this example) has the opposite effect.
public boolean checkDataByFilter(Map<String, String> filterValue, boolean equal) { boolean same = true; boolean different = false; try { Thread.sleep(3000); } catch (Exception e) {} List<Map<String, String>> mapList = getData(); for (Map<String, String> map : mapList) { for (String key : filterValue.keySet()) { if (!map.containsKey(key) || !map.get(key).equals(filterValue.get(key))) { if (equal) { same = false; } else { different = true; } break; } } if (!same || different)
To read the full article click on the 'post' link at the top.