SlideShare uma empresa Scribd logo
1 de 23
Mocks with Mockito API




      Camilo Lopes
   www.camilolopes.com
Agenda
•   About Mocks
•   Mocks API
•   Mockito API
•   Mocks with Mockito
•   Conclusion
What Mock is?
In one setence

“Mocks are object that simulates
 the behavior of real an object.”
Why Mocks?
•   Help design solution;
•   Avoid high coupling;
•   Promove high cohesion;
•   Easy to use;
•   Promove Agile development;
•   Improve performance in unit test;
Where can I use Mock?
•   DataBase;
•   WebServices;
•   NetWork
•   Classes;
How to use Mock?
Mocks API
What is the best?
In my opinion is...
Why?
Because...
Mockito API
•   Easy to use;
•   High learning Curve;
•   Excellent Documentation;
•   “stub-run-verify” concept;
•   Concept stub is separed of verification;
•   Clear code;
And the others?
•   Use “except-run-verify” concept; (it is not good)
•   Code is not as clear as Mockito;
•   Documentation is not as good as Mockito ‘s doc;
•   Verify and expect are bult-in in expect() method;
    (Mockito is separated. It is better)
Mockito Features
  Examples ahead
Exception
when(myobjectMock.buy(new BigDecimal("-5000.00"))).thenThrow(new
  Exception());
Matchers
when(myobjectMock.buy(any(BigDecimal.class)).thenReturn(true);


                     Note
Mockito offers some built-in such as: anyString,
 anyInt(), any(class). But we can create
 customized built-in. Our class must extends
 ArgumentMatcher.
void method
• doAnswer, doNothing, doThrow or doReturn

doThrow(new Exception()).when(mockObject).buy(new
  BigDecimal("-5000"))
Verify
  it possible check the number invocation of method
• atLeastOnce(), atLeast(int), atMost(int), never()

verify(mockobject,times(3)).buy(new BigDecimal("10000"))
More features
Check Mockito Documentation. There are many
  examples.

http://docs.mockito.googlecode.com/hg/org/mockito/
  Mockito.html
Conclusion
If you want to improve of quality in unit test use
   Mock.It will contribute to design, code,
   development etc.There are many APIs in
   market, but the most used are: EasyMock,
   Jmock and Mockito.
Thanks!!!!
References
http://code.google.com/p/mockito/
http://www.infoq.com/news/2008/06/mocks-q-of-when
http://www.easymock.org/
http://martinfowler.com/articles/mocksArentStubs.html

Mais conteúdo relacionado

Mais procurados

Interaction testing using mock objects
Interaction testing using mock objectsInteraction testing using mock objects
Interaction testing using mock objects
Lim Chanmann
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
TO THE NEW | Technology
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Harry Potter
 

Mais procurados (20)

Power mock
Power mockPower mock
Power mock
 
Basic Unit Testing with Mockito
Basic Unit Testing with MockitoBasic Unit Testing with Mockito
Basic Unit Testing with Mockito
 
Mockito intro
Mockito introMockito intro
Mockito intro
 
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mock
 
Mockito intro
Mockito introMockito intro
Mockito intro
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock Tutorial
 
JMockit Framework Overview
JMockit Framework OverviewJMockit Framework Overview
JMockit Framework Overview
 
EasyMock for Java
EasyMock for JavaEasyMock for Java
EasyMock for Java
 
Easy mock
Easy mockEasy mock
Easy mock
 
Testdriven Development using JUnit and EasyMock
Testdriven Development using JUnit and EasyMockTestdriven Development using JUnit and EasyMock
Testdriven Development using JUnit and EasyMock
 
Programmer testing
Programmer testingProgrammer testing
Programmer testing
 
Interaction testing using mock objects
Interaction testing using mock objectsInteraction testing using mock objects
Interaction testing using mock objects
 
Mastering Mock Objects - Advanced Unit Testing for Java
Mastering Mock Objects - Advanced Unit Testing for JavaMastering Mock Objects - Advanced Unit Testing for Java
Mastering Mock Objects - Advanced Unit Testing for Java
 
Easy mockppt
Easy mockpptEasy mockppt
Easy mockppt
 
Unit testing basic
Unit testing basicUnit testing basic
Unit testing basic
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
Unit test-using-spock in Grails
Unit test-using-spock in GrailsUnit test-using-spock in Grails
Unit test-using-spock in Grails
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Writing good unit test
Writing good unit testWriting good unit test
Writing good unit test
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 

Destaque (7)

Mockito (JUG Latvia)
Mockito (JUG Latvia)Mockito (JUG Latvia)
Mockito (JUG Latvia)
 
AWS User Group Meetup Berlin - Kay Lerch on Apache NiFi (2016-04-19)
AWS User Group Meetup Berlin - Kay Lerch on Apache NiFi (2016-04-19)AWS User Group Meetup Berlin - Kay Lerch on Apache NiFi (2016-04-19)
AWS User Group Meetup Berlin - Kay Lerch on Apache NiFi (2016-04-19)
 
Dataflow with Apache NiFi - Crash Course - HS16SJ
Dataflow with Apache NiFi - Crash Course - HS16SJDataflow with Apache NiFi - Crash Course - HS16SJ
Dataflow with Apache NiFi - Crash Course - HS16SJ
 
Docker 基礎介紹與實戰
Docker 基礎介紹與實戰Docker 基礎介紹與實戰
Docker 基礎介紹與實戰
 
Hadoop Summit Tokyo Apache NiFi Crash Course
Hadoop Summit Tokyo Apache NiFi Crash CourseHadoop Summit Tokyo Apache NiFi Crash Course
Hadoop Summit Tokyo Apache NiFi Crash Course
 
Taking DataFlow Management to the Edge with Apache NiFi/MiNiFi
Taking DataFlow Management to the Edge with Apache NiFi/MiNiFiTaking DataFlow Management to the Edge with Apache NiFi/MiNiFi
Taking DataFlow Management to the Edge with Apache NiFi/MiNiFi
 
Jupyter 簡介—互動式的筆記本系統
Jupyter 簡介—互動式的筆記本系統Jupyter 簡介—互動式的筆記本系統
Jupyter 簡介—互動式的筆記本系統
 

Semelhante a Mock with Mockito

Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Tony Nguyen
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Young Alista
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Luis Goldster
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Fraboni Ec
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
James Wong
 

Semelhante a Mock with Mockito (20)

CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
 
Top Testing Tips
Top Testing TipsTop Testing Tips
Top Testing Tips
 
Just Mock It - Mocks and Stubs
Just Mock It - Mocks and StubsJust Mock It - Mocks and Stubs
Just Mock It - Mocks and Stubs
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Bridging the communication Gap & Continuous Delivery
Bridging the communication Gap & Continuous DeliveryBridging the communication Gap & Continuous Delivery
Bridging the communication Gap & Continuous Delivery
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applications
 
1_mockito
1_mockito1_mockito
1_mockito
 
Understanding Mocks
Understanding MocksUnderstanding Mocks
Understanding Mocks
 
ASP.NET MVC Best Practices malisa ncube
ASP.NET MVC Best Practices   malisa ncubeASP.NET MVC Best Practices   malisa ncube
ASP.NET MVC Best Practices malisa ncube
 
Integrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMSIntegrating AngularJS into the Campus CMS
Integrating AngularJS into the Campus CMS
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 

Mais de Camilo Lopes

Apresentando Scrum ao cliente
Apresentando Scrum ao clienteApresentando Scrum ao cliente
Apresentando Scrum ao cliente
Camilo Lopes
 
ITS de Startup para Startups
ITS  de Startup para StartupsITS  de Startup para Startups
ITS de Startup para Startups
Camilo Lopes
 
Start-up adotando Scrum
Start-up adotando ScrumStart-up adotando Scrum
Start-up adotando Scrum
Camilo Lopes
 

Mais de Camilo Lopes (19)

Mitos e verdades startup
Mitos e verdades startupMitos e verdades startup
Mitos e verdades startup
 
Usecash - Meio de Pagamento através do Troco
Usecash - Meio de Pagamento através do TrocoUsecash - Meio de Pagamento através do Troco
Usecash - Meio de Pagamento através do Troco
 
Tdc2014 scrum remoto rola ou nao rola
Tdc2014  scrum remoto rola ou  nao rolaTdc2014  scrum remoto rola ou  nao rola
Tdc2014 scrum remoto rola ou nao rola
 
Apresentando Scrum ao cliente
Apresentando Scrum ao clienteApresentando Scrum ao cliente
Apresentando Scrum ao cliente
 
ITS de Startup para Startups
ITS  de Startup para StartupsITS  de Startup para Startups
ITS de Startup para Startups
 
Overview about AngularJS Framework
Overview about AngularJS Framework Overview about AngularJS Framework
Overview about AngularJS Framework
 
Start-up adotando Scrum
Start-up adotando ScrumStart-up adotando Scrum
Start-up adotando Scrum
 
TDD na Prática
TDD na PráticaTDD na Prática
TDD na Prática
 
Como ser escritor no brasil
Como ser escritor no brasilComo ser escritor no brasil
Como ser escritor no brasil
 
Boas práticas com TDD
Boas práticas com TDD Boas práticas com TDD
Boas práticas com TDD
 
TDC 2012 TDD e 20 coisas que você precisa saber
TDC 2012 TDD e 20 coisas que você precisa saberTDC 2012 TDD e 20 coisas que você precisa saber
TDC 2012 TDD e 20 coisas que você precisa saber
 
Scrum in few minutes
Scrum in few minutesScrum in few minutes
Scrum in few minutes
 
Convencendo o cliente usar Scrum 5min
Convencendo o cliente usar Scrum 5minConvencendo o cliente usar Scrum 5min
Convencendo o cliente usar Scrum 5min
 
Guia JEE com Frameworks
Guia JEE com FrameworksGuia JEE com Frameworks
Guia JEE com Frameworks
 
Palestra Exchange Program - NewZealand
Palestra Exchange Program - NewZealandPalestra Exchange Program - NewZealand
Palestra Exchange Program - NewZealand
 
Guia de Bolso SCJP
Guia de Bolso SCJPGuia de Bolso SCJP
Guia de Bolso SCJP
 
Certificacao Java
Certificacao JavaCertificacao Java
Certificacao Java
 
Seminario Refatoracao
Seminario RefatoracaoSeminario Refatoracao
Seminario Refatoracao
 
Refactoring
RefactoringRefactoring
Refactoring
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

Mock with Mockito