Unit Tests
Integration Tests
= small, separate, isolated
= integration of units
vs
Benefits
simplify integration
eliminate regressions
working API docs
confidence!
JUnit 4
import static org.junit.Assert.*;
@Test
@Test(expected=Exception.class)
@Before
@After
@BeforeClass
@AfterClass
@Ignore
Best practices
keep test cases short
proper names,
so the intent is clear
3 parts
preparation
invocation
verification
avoid "test everything"
in a single test case
avoid code duplication
BUT!
what if the code is hard to test?
refactor!
or better avoid with...
TDD
Test
Driven
Development
... but who should run the tests?
tests before the functional code
functional code only to satisfy failing tests
small steps
early API design
loosely coupled code
100% coverage
no overengineering
YOU!
... and CI server!
Anton Keks
anton@codeborne.com
but what exactly
should I test?..
everything you can!
...and more
all your code is supposed to work, right?
Mocks are your best friends
for keeping tests UNIT!
use Mockito!
rename, extract method, extract interface, ...
of Unit Tests