LOGO SPEAKER‘S COMPANY
The Charm of MockitoTest Spies in Action
Szczepan Faber
Sabre Holdings
LOGO SPEAKER‘S COMPANY
2
whoami
> Developer
> Coach
> Open source contributor
> Bartender who serves you Mockito
> Works for Sabre Holdings in Krakow/Poland
LOGO SPEAKER‘S COMPANY
3
agenda
> The charm in action: less slides more action
> Test-driving with Mockito and classic mocking tools
> My goal is: to help you write high quality tests that push high
quality application code.
LOGO SPEAKER‘S COMPANY
4
what are mocks?
> (simplification warning) Substitutes of real objects for purposes of testing
> What libraries do you use?
> Ok, Szczepan, enough talking, start coding
LOGO SPEAKER‘S COMPANY
5
the charm of Mockito
> intuitive
> explict
> tries very hard not to produce brittle tests
> enables given-when-then BDD-style tests
> separates stubbing from verification
> has good point of failure
> produces clean stack trace
> has @Mock annotation
LOGO SPEAKER‘S COMPANY
6
the charm of Mockito
> inOrder() verification
> atLeast(x), times(x), etc. verification
> verifyNoMoreInteractions()
> consecutive stubbing: thenReturn(x).thenThrow(y)
> stubbing with generic Answers
> flexible arguments matching: anyObject(), etc.
> integrated with hamcrest matchers
LOGO SPEAKER‘S COMPANY
7
the charm is working
> Mockito for python
> Mockito for flex
> Mockito-like library for javascript
> Mockito-like library for C++
> Spying in .Net
> Mockito extensions
> The user base...
LOGO SPEAKER‘S COMPANY
8
what I don’t like about Mockito
> a bit inconsistent API:
verify(mock).method();
when(mock.method()).thenReturn(x);
> stubbing voids is different:
doThrow(ex).when(mock).method();
> may lead to overmocking because it’s too easy to mock ☺
LOGO SPEAKER‘S COMPANY
9
Your takeaway from this session
You know how to increase quality of your code with Mockito library + BDD test
template
You learned the API of mocking tools
You learned about mock objects & test spies
LOGO SPEAKER‘S COMPANY
10
feedback
> szczepiq<at>gmail.com
> mockito mailing lists (http://mockito.org)
> feel free to ask after the session or anytime during the conference
LOGO SPEAKER‘S COMPANY
11
regards
jMock guys for Mock Objects
EasyMock guys for their innovative syntax
Gerard Maszeros for sorting out mocking terminology
Mockito users and contributors for their ideas
You guys for surviving this session
LOGO SPEAKER‘S COMPANY
12
last slide (I promise)Sorry if I misused EasyMock or jMock...
Do TDD and write great tests with whatever tool that works for you!
Yummy. No hangover
Useful links:
- Martin Fowler ‘Mocks arent Stubs’
- wiki on BDD, Dan North’s blog
- Paul Szulc blog entry on the ‘test template’
- xUnit patterns by Gerard Maszeros
- mockobjects.org
- mockito.org
- Brett Schuchert tutorial on Mockito
- Infinitest.org
LOGO SPEAKER‘S COMPANY
Szczepan Faber http://mockito.org
Sabre Holdings szczepiq<at>gmail.com
Top Related