junit_best_practices
JUnit Best Practices
By Kyle Blaney
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 ofassertEquals(…)
- 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 →
@Beforeinstead - 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
Exceptioninstead
- 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
