====== JUnit Best Practices ====== By Kyle Blaney http://www.kyleblaney.com/junit-best-practices/ ====== Conspect ====== * Completely in memory * Don't skip * One test method - one assertion * Use strongest assertion possible * strongest * strong * weak * non-existent * ... * Use most appropriated assertion methods * e.g. ''assertTrue(...)'' instead of ''assertEquals(...)'' * Use exact matching when using mocking parameter * e.g. avoid Mockito's any-Methods * Name unit tests using a convention * ''_'' * test and production classes should be in one package * test and production code separation * Maven does it normally * No println's in tests * No constructors -> ''@Before'' instead * Don't use static members * No own catch blocks to fail a test * No own catch blocks to pass a test * use ''@Test(expected=IOException.class)'' instead * No own catch blocks to print stack traces * No methods in Test classes that throws any particular type of exception * method under test can change their signature! * use''Exception'' instead * Don't use ''Thread.sleep()'' * allow injection of mock object that simulates long running operation * No indirect tests * test dependent classes directly