User Tools

Site Tools


junit_best_practices

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
    • <methodUnderTest>_<condition>
  • 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!
    • useException instead
  • Don't use Thread.sleep()
    • allow injection of mock object that simulates long running operation
  • No indirect tests
    • test dependent classes directly
junit_best_practices.txt · Last modified: by kalaschnikow