don't give up on mocking - · pdf filedon’t give up on mocking-why do people give...
TRANSCRIPT
![Page 1: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/1.jpg)
Don’t give up on mocking
- Why do people give up?
- Mocking: the big step from classic way of testing
- Let’s take a step back and don’t give up!
by Szczepan Faber (a certified mock tamer)
![Page 2: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/2.jpg)
Interaction testing...
� State testing is asking: „what’s your colour, Mr Object?”
� Interaction testing is asking: „Mrs Object, what did you say to Mr Object?”
![Page 3: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/3.jpg)
The language
� The natural language of state testing are assertions
� The natural language of interaction testing is... mocking?
![Page 4: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/4.jpg)
What’s a mock or a stub?
� It is a substitue of the real thing for the purposes of testing
![Page 5: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/5.jpg)
Mocking...
� Is it a design tool for describing messaging patterns between abstract state machines?
� Is it a handy tool which lets me create mocks dynamically?
![Page 6: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/6.jpg)
Giving up...
� The internet says mocking is cool
� Let’s find out why one would give up on mocking!
![Page 7: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/7.jpg)
Why would one give up on mocking?
![Page 8: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/8.jpg)
Why would one give up on mocking?
� because aggressive validation makes the tests brittle �
![Page 9: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/9.jpg)
The code
![Page 10: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/10.jpg)
The test
![Page 11: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/11.jpg)
And the lovely green bar
![Page 12: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/12.jpg)
TDD-ing a new feature (test)
![Page 13: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/13.jpg)
The adorable red bar
![Page 14: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/14.jpg)
TDD-ing a new feature (code)
![Page 15: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/15.jpg)
Whoah? Red bar again?
![Page 16: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/16.jpg)
Why would one give up on mocking?
� because I have to fix tests even when the code is not broken:
� may increase noise
� may lead to overspecification
![Page 17: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/17.jpg)
Fixing by ignoring interactions
![Page 18: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/18.jpg)
Fixing by adding required expectation
![Page 19: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/19.jpg)
Why would one give up on mocking?
� What if hand mocks were better?
![Page 20: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/20.jpg)
Remember the code?
![Page 21: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/21.jpg)
Let’s try some hand written mocks
![Page 22: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/22.jpg)
By hand or with the framework: the essence
![Page 23: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/23.jpg)
By hand or with the framework: expectations
![Page 24: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/24.jpg)
Complete test
![Page 25: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/25.jpg)
Why would one give up on mocking?
� Let’s look at the point of failure
![Page 26: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/26.jpg)
Point of failure and hand mocks
Hand mocks show useful stack trace pointing to exact line of code
![Page 27: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/27.jpg)
When the framework fails on verify()
The exception message which tries to be readable.
![Page 28: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/28.jpg)
When the framework fails with
„Unexpected Interaction!”
Helpful but...
![Page 29: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/29.jpg)
Ok, now I understand why one would
give up on mocking.
� because aggressive validation makes the tests brittle �
� because I have to fix tests even when the code is not broken
� but it can increase noise
� or lead to overspecification
� because hand-mocks can be considered better:
� less noisy
� more natural
� with better(?) point of failure
![Page 30: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/30.jpg)
Are hand mocks a better option, then?
� Err... not really... hand mocks have different issues.
� Hand mocks bad, mocking framework bad what should I do now?
![Page 31: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/31.jpg)
A taste of Mockito, a Test Spy framework
![Page 32: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/32.jpg)
A taste of hand mocks, no framework at all
![Page 33: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/33.jpg)
Test Spy framework
� because aggressive validation makes the tests brittle �
� because I have to fix tests even when the code is not broken
� but it can increase noise
� or lead to overspecification
� because hand-mocks can be considered better:
� less noisy
� more natural
� with better(?) point of failure
![Page 34: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/34.jpg)
Languages, where are your Test Spy
frameworks?
� You’ve got plenty of mocking frameworks
� Java
� C#
� Ruby
� Python
� JavaScript
� But you’ve got so little Test Spy frameworks� Java
� C#
� Ruby
� Python
� JavaScript
![Page 35: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/35.jpg)
This is what is trendy in the mocking
world these days
� Better and better DSLs for describing expectations
� Partial mocking
� Mocking static methods
� Features that solve rare corner cases
� Etc.
![Page 36: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/36.jpg)
Mock objects: the quest for quality
� Does application code quality vary when using different mock libraries (or hand mocks)?
� Does test code quality vary when using different mock libraries (or hand mocks)?
� Can I use different mock libraries in single project?
![Page 37: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/37.jpg)
Mocking in Java
� jMock
� EasyMock
� Mockito
![Page 38: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/38.jpg)
How to verify the method was called?
JMock:
EasyMock:
Mockito:
![Page 39: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/39.jpg)
How to tell a method to return a value?
JMock:
EasyMock:
Mockito:
![Page 40: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/40.jpg)
How verify the method was not called
JMock:
EasyMock:
(always implicit)
Mockito:
![Page 41: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/41.jpg)
Mockito separates stubbing from
verification
![Page 42: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/42.jpg)
Classic mocking doesn’t separate
stubbing from verification
JMock:
EasyMock:
![Page 43: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/43.jpg)
Mockito knows developers read stack trace
![Page 44: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/44.jpg)
Mockito knows developers read stack trace
![Page 45: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/45.jpg)
Mockito is a Test Spy framework
run()
verifyThis()
verifyThat()
Spying
run()
assertThis()
assertThat()
expectThis()
expectThat()
run()
verify()
Classic testingClassic mocking
![Page 46: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/46.jpg)
Mockito and classic testing are explicit
strict style requires explicit specification:
assertNotTrue(something);verify(mock, never()).method();
loose style requires explicit specification:
ignoreInteractions(mock);
loose by defaultstrict by default
Classic testing and MockitoClassic mocking
![Page 47: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/47.jpg)
The current era in my project is Mockitozoic!
� jMockozoic ->
� EasyMockozoic ->
� HandMockozoic ->
� Mockitozoic
![Page 48: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/48.jpg)
What’s next?
� jMockozoic ->
� EasyMockozoic ->
� HandMockozoic ->
� Mockitozoic ->
� ?
![Page 49: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/49.jpg)
What I don’t like about Mockito
� a bit inconsistent API:� verify(mock).method();
� stub(mock.method()).toReturn(x);
� stubbing voids is different:� doThrow(ex).when(mock).method();
� may lead to overmocking because it’s too easy to mock ☺
![Page 50: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/50.jpg)
What users like about Mockito?
� explicit API
� flexible verification
� separation of stubbing and verification
� @Mock annotation
� expectations after exercising
![Page 51: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/51.jpg)
What are the plans for Mockito:
� maintain slip API to promote simple code
� change the stubbing api:� instead: stub(mock.getStuff()).toReturn(x);
� do: when(mock.getStuff()).thenReturn(x);
� spread to other languages (python, c++, C#)
![Page 52: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give](https://reader031.vdocument.in/reader031/viewer/2022030405/5a7e30f07f8b9a49588e35cb/html5/thumbnails/52.jpg)
Regards
� jMock guys for inventing mock objects
� EasyMock guys for their innovative syntax
� Gerard Maszeros for sorting out mocking terminology
� Mockito users and contributors for their ideas