the charm of mockito€¦ · the charm of mockito > intuitive > explict > tries very hard...

13
LOGO SPEAKER‘S COMPANY The Charm of Mockito Test Spies in Action Szczepan Faber Sabre Holdings

Upload: others

Post on 15-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

LOGO SPEAKER‘S COMPANY

The Charm of MockitoTest Spies in Action

Szczepan Faber

Sabre Holdings

Page 2: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

LOGO SPEAKER‘S COMPANY

2

whoami

> Developer

> Coach

> Open source contributor

> Bartender who serves you Mockito

> Works for Sabre Holdings in Krakow/Poland

Page 3: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

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.

Page 4: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

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

Page 5: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

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

Page 6: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

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

Page 7: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

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...

Page 8: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

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 ☺

Page 9: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

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

Page 10: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

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

Page 11: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

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

Page 12: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

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

Page 13: The Charm of Mockito€¦ · the charm of Mockito > intuitive > explict > tries very hard not to produce brittle tests > enables given-when-then BDD-style tests > separates stubbing

LOGO SPEAKER‘S COMPANY

Szczepan Faber http://mockito.org

Sabre Holdings szczepiq<at>gmail.com