Tomek Kaczanowski - Practical Unit Testing With Testng and Mockito - 2012
Mockito (JUG Latvia)
-
Upload
dmitry-buzdin -
Category
Technology
-
view
1.590 -
download
0
description
Transcript of Mockito (JUG Latvia)
![Page 1: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/1.jpg)
Mockito
JUG LatviaDenis Udod
![Page 2: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/2.jpg)
Mochito vs Mockito
Mochito Recipe:
1/12 liter Dark Rum 3 cl Lemon Juice 1/2 handful Mint 1/12 liter Club Soda 8 drops Angostura Bitters
![Page 3: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/3.jpg)
Mochito vs Mockito
Mochito Recipe:
1/12 liter Dark Rum 3 cl Lemon Juice 1/2 handful Mint 1/12 liter Club Soda 8 drops Angostura Bitters
Mockito Recipe:
Java Unit testing Mock object
![Page 4: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/4.jpg)
Mochito vs. Mockito
Mochito Recipe:
1/12 liter Dark Rum 3 cl Lemon Juice 1/2 handful Mint 1/12 liter Club Soda 8 drops Angostura Bitters
Mockito Recipe:
Java Unit testing Mock object
What is the difference?
![Page 5: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/5.jpg)
Mockito: Why drink it?
Q: What is unit testing?
Q: What is a mock-objects?
Q: Why do we mock-objects?
![Page 6: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/6.jpg)
Unit testing - What's the point?
Write simple test for each class method.
![Page 7: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/7.jpg)
Unit testing - What's the point?
Write simple test for each class method.
![Page 8: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/8.jpg)
Unit testing - What's the point?
Write simple test for each class method.
![Page 9: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/9.jpg)
Unit testing - What's the point?
Write simple test for each class method.
• simple• fast• independent from "any" (database, network, filesystem)
![Page 10: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/10.jpg)
Mock object - What's the point?
Simulate the behavior of complex, real(non-mock) objects.
• Useful when a real object is impractical or impossible to incorporate into a unit test.
![Page 11: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/11.jpg)
Mock object - What's the point?
Example - service object supplies non-deterministic results:
![Page 12: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/12.jpg)
Mock object - What's the point?
Example - service object supplies non-deterministic results:
![Page 13: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/13.jpg)
Mockito: Why drink it?
• Mockito is a mocking framework that tastes really well.
• It lets you write beautiful tests with clean & simple API.
• Mockito doesn't give you hangover.
![Page 14: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/14.jpg)
Mockito: Why drink it?
Syntax
• Let’s keep it simple• No Strings for methods.• No anonymous inner classes.• Painless refactoring. Renaming a method should not break
my tests.
![Page 15: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/15.jpg)
Mockito: How to drink it?
1. Download from http://mockito.org 2. Add to project.3. And have fun
![Page 16: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/16.jpg)
Mockito: How to drink it?
Create mock object:
![Page 17: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/17.jpg)
Mockito: How to drink it?
Create mock object:
![Page 18: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/18.jpg)
Mockito: How to drink it?
Create mock object:
![Page 19: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/19.jpg)
Mockito: How to drink it?
How about returned values?
![Page 20: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/20.jpg)
Mockito: How to drink it?
How about returned values?
![Page 21: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/21.jpg)
Mockito: How to drink it?
Check method invocations
![Page 22: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/22.jpg)
Mockito: How to drink it?
Check method invocations
![Page 23: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/23.jpg)
Mockito: How to drink it?
Check method invocations
![Page 24: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/24.jpg)
Mockito: How to drink it?
Check method invocations
![Page 25: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/25.jpg)
Mockito: How to drink it?
Check method invocations
![Page 26: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/26.jpg)
Mockito: How to drink it?
Spying on real object:
![Page 27: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/27.jpg)
Mockito: Hangover?
Limitation:
• needs java 1.5+• Cannot mock final class• Cannot mock static, final methods• Cannot mock equals(),hashCode() metods• Cannot mock private metods• Cannot verify toString()
![Page 28: Mockito (JUG Latvia)](https://reader033.fdocuments.in/reader033/viewer/2022061217/54b502c54a7959cc7c8b4748/html5/thumbnails/28.jpg)
MockitoWho is your bartender?
Mockito is served to you by Szczepan Faber and friends.www.mockito.org