100 Java Mistakes and How to Avoid Them

Unit testing

  • Avoid side effects inside?assert?statements, as expressions inside them are not evaluated at all when assertions are turned off.?
  • Try not to rely on weak assertion methods, like?assertNotNull()?and?assertNotEquals(). If such assertions pass, it doesn’t mean much, as there could easily be a mistake in the test itself.?
  • Ensuring in tests that a given method throws an exception was tricky in older versions of test frameworks. This is no longer a problem with new methods, like?assertThrows(). Use modern API methods to write more robust tests.?
  • Returning from the test method prematurely on some condition is a bad sign. If more assertions will be added at the end of the method later, they might be not checked when preliminary return takes place.?
  • Test frameworks require some extra attention to detail to make tests work, like adding?@Testannotation or declaring the test method public. Make sure you fulfill all the requirements; otherwise, your test will not be executed at all.

#Booksummary

Credit : Manning


要查看或添加评论,请登录

Pankaj Gajjar的更多文章

社区洞察

其他会员也浏览了