SlideShare una empresa de Scribd logo
1 de 35
Descargar para leer sin conexión
copyright 2008 trainologic LTD
Unit testing in a real-life environment using
Mockito
Stubbing and Mocking
copyright 2008 trainologic LTD
Stubbing and Mocking
• Mediator Objects and Testing
• Stubs
• Mocking Objects
• Mockito
• PowerMockito
3
copyright 2008 trainologic LTD
• The JUnit testing framework, makes the testing of an
independent, non-void method, simple.
• But... how do we test an method with no return value?
• How do we test a method that interacts with other
domain objects?
Testing Mediator Objects
3
Mediator Objects & Testing
4
copyright 2008 trainologic LTD
Mediator Objects & Testing
• Dependencies – In order for a function to run it often
requires Files, DB, JNDI or generally – other units
• Side effects – As a function runs we are often interested
in data written to files, saved to db or generally –
passed to other units
How can we test a single unit, without being
affected by other units – their errors, set-up
complexities and performance issues?
In Real-Life Units are NOT Totally
Self Contained
4
5
copyright 2008 trainologic LTD
• Let’s say we want to test a method that gets an
Account and a Financial Transaction, checks whether
the Transaction is legitimate and only if it is, applies it
to the Account.
• How would you implement this test?
Testing Mediator Objects
5
Mediator Objects & Testing
6
copyright 2008 trainologic LTD
• What if the method receives an Account and prints
specific parts of it using a Logger?
Testing Mediator Objects
6
Mediator Objects & Testing
7
copyright 2008 trainologic LTD
• Many business objects that are really important to test
are mediators, i.e., they contain methods which interact
with other domain objects.
• Since in unit testing we want to test a unit without its
dependencies, we will have to eliminate them somehow.
• We do this by passing dummy objects to “fill in” for the
real dependencies.
Testing Mediator Objects
7
Mediator Objects & Testing
copyright 2008 trainologic LTD
Stubbing and Mocking
• Mediator Objects and Testing
• Stubs
• Mocking Objects
• Mockito
• PowerMockito
9
copyright 2008 trainologic LTD
• A Stub is a test-only object which:
• Implements the same interface as the production
object
• Gives the set of services used by the tested unit –
often using a naïve, hard-coded implementation
• More complex stubs may even contain test-supporting
logic, e.g. assert that they are used correctly
What is a Stub?
9
Stubs
copyright 2008 trainologic LTD
• BookInventory is a package for management of … book
inventories.
• It does inventory management, connection to book
catalogs (like library of congress), etc.
Exercise 1- BookInventory
Stubs
11
copyright 2008 trainologic LTD
• Main Classes
Exercise 1- BookInventory
11
Stubs
1111
BookInventory
Main entry point
BookCatalog
Get title data
Amazon
Get title data
LibraryOfCongress
Get title data
ChristianCatalog
Get title data
BookCopy
Main entry point
*
12
copyright 2008 trainologic LTD
• Write a unit test for BookInventory.registerCopy
• A “good” copy can be registered
• Several copies can be registered for both same and
different titles
• A copy can only be registered if the title details are
known
• Same copy cannot be registered twice
• Same copy id for two different titles gives specific
error message
Exercise 1- BookInventory
12
Stubs
13
copyright 2008 trainologic LTD
• Pros:
• We can write anything
• Cons:
• A lot of work
• Error prone
• Gets complicated as our demands increase:
• Was the catalog actually called?
• Was it called more than once?
• Was the isbn passed correctly to the catalog?
• What if two catalogs return different results?
Stubbing Pros and Cons
13
Stubs
copyright 2008 trainologic LTD
Stubbing and Mocking
• Mediator Objects and Testing
• Stubs
• Mocking Objects
• Mockito
• PowerMockito
15
copyright 2008 trainologic LTD
• A Mock object is an object that:
• Is created by the mock framework
• Implements the same interface as the original
dependent object. Otherwise, the compiler won’t let
us pass it to the tested object.
• Supplies the tested code with everything it expects
from the dependent object.
• Allows us to check that the tested code is using the
dependent object (the Mock) correctly.
Mock Objects
15
Mocks
16
copyright 2008 trainologic LTD
• To use a Mock object we should:
• Create instance of the Mock.
• Define bahavior during test.
• Invoke the tested code while passing the Mock as
parameter.
• Verify the Mock has been used correctly.
• Let’s see an example...
Introduction to Mock Objects
16
Mocks
17
copyright 2008 trainologic LTD
17
Mocks
Example: Testing book addition
with Mock Title Catalog
Construct
Set behavior
Verify behavior
18
copyright 2008 trainologic LTD
• Setup mocks for all dependencies required by our units
– Either as part of setUp (@Before) or at the beginning
of the test method
• Set mocks to correctly handle interaction – Usually at
the beginning of the test method
• Call business logic under testing
• Verify results & behavior (i.e. “side effects”)
The Typical Usage Pattern
18
Mocks
19
copyright 2008 trainologic LTD
• When we set expectations on unit behavior, we are also
setting restrictions on how it is implemented
• It is very easy to create tests that are implementation
specific, and will break even though the unit is OK
• Such tests are called “brittle” and they are nightmare to
maintain
The danger of testing behavior
through mocks
19
Mocks
20
copyright 2008 trainologic LTD
• Testing the SQL passed to the DB
• Verifying that the unit actually pulls data given by
mocks
• Testing for a specific order of calls to mocks
• Etc.
Some Examples of Brittle tests
20
Mocks
Over-Specified behavior == Brittle tests
copyright 2008 trainologic LTD
Stubbing and Mocking
• Mediator Objects and Testing
• Stubs
• Mocking Objects
• Mockito
• PowerMockito
22
copyright 2008 trainologic LTD
Mockito
22
• An open-source project providing an easy way to work
with Mock objects.
• Can be freely downloaded from Google Code
http://code.google.com/p/mockito.
• Released under the MIT License.
Mockito
23
copyright 2008 trainologic LTD
• No need to write Mock objects by hand.
• Simple “Set-Run-Verify” work model (as opposed to
(expect/record-run-verify)
• Refactoring-safe, no need to refactor Mock in case the
interface has changed.
• Supports return values and Exceptions.
• Flexible parameter handling in both set & verify
• Single jar, easy setup
• Easy to learn
Mockito - Benefits
23
Mockito
24
copyright 2008 trainologic LTD
• Import Mockito – preferably static:
import static org.mockito.Mockito.*;
•Option 1: “mock” instead of “new”
IAccount mockAct= mock(IAccount.class);// or even…
Account mockAct= mock(Account.class); //on a concrete class
•Option 2: @mock a member
@mock private Account mockAct;
(But then you need to use MockitoAnnotations.initMocks or
JUnitMockitoRunner)
Constructing Mocks
24
Mockito
25
copyright 2008 trainologic LTD
• Examples:
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(or(eq(1), eq(2)))). thenReturn(“more");
when(mockedList.get(gt(2)).thenThrow(new RuntimeException());
• Other Matchers:
• anyInt, anyString,…
• Custom matchers using hamcrest
Setting up Mock Behavior
25
Mockito
26
copyright 2008 trainologic LTD
• Verify a function was called:
verify(mockedList).clear();
•Verify parameters passed:
verify(mockedList).add("one");
verify(mockedList).add(anyString());
•Verify number of invocations:
verify(mockedList,atLeastOnce()).add("one");
verify(mockedList,times(3)).add(anyString());
similarly: never, atLeast, atMost
•CAUTION: This is the heart of brittle testing!
Verifying Behavior
26
Mockito
27
copyright 2008 trainologic LTD
• Using Mockito:
• Rewrite the tests from Exercise 1
• Also test that:
• BookInventory.registerCopy:
• Works well with several TitleCatalog objects
• Stops searching for book details once it has
them
• Extend BookInventory to support TitleCatalogs of
banned books. Test it.
(A copy of a banned book cannot be registered)
Exercise 2
27
Mockito
28
copyright 2008 trainologic LTD
• Spy allows us to wrap a real object, and perform partial
mocking
• This is dangerous,
but may be relevant
with some legacy
code.
Partial Mocking: Spy Objects
28
Mockito
29
copyright 2008 trainologic LTD
• Mockito allows you to verify the order in which methods
on mocks were called.
• This is yet another example of how you can create
extremely brittle tests. Use with caution.
Verifying call order
29
Mockito
30
copyright 2008 trainologic LTD
• You can make sure a mock was not used through
verifyZeroInteractions
•You can make sure a mock was not used following
a specific verify through
verifyNoMoreInteractions
•Both are often over specification…
Expecting Nothing is Expecting Something
30
Mockito
copyright 2008 trainologic LTD
Stubbing and Mocking
• Mediator Objects and Testing
• Stubs
• Mocking Objects
• Mockito
• PowerMockito
copyright 2009 Trainologic LTD
• PowerMock is a framework that extends
Mockito/EasyMock. For Mockito it is called
PowerMockito.
• PowerMock uses a custom classloader and bytecode
manipulation to enable mocking of static
methods, constructors, final classes and
methods, private methods, and more.
• This is a mixed blessing:
• We can avoid re-factoring legacy code in order to
test it
• We are less inclined to fix bad design
PowerMock
EasyMock
copyright 2009 Trainologic LTD
• Lets see an example:
• We have the following class:
PowerMockito Example
EasyMock
copyright 2009 Trainologic LTD
PowerMock Example
EasyMock
35
copyright 2008 trainologic LTD
EasyMock
• In order to unit-test isolated units we must use Mock
objects.
• Writing Mocks by hand is cumbersome.
• Mockito is a simple and easy to use library that helps us
create Mock implementations on the fly.
How do you know if a person really does unit testing?
Ask him what mocking framework he is using!
Summary
35

Más contenido relacionado

La actualidad más candente

Spring boot
Spring bootSpring boot
Spring bootsdeeg
 
Unit testing framework
Unit testing frameworkUnit testing framework
Unit testing frameworkIgor Vavrish
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockitoshaunthomas999
 
Google test training
Google test trainingGoogle test training
Google test trainingThierry Gayet
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkOnkar Deshpande
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsYura Nosenko
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test frameworkAbner Chih Yi Huang
 
PRESENTATION_CHAABA_AYOUB_ING5_ISI.pptx
PRESENTATION_CHAABA_AYOUB_ING5_ISI.pptxPRESENTATION_CHAABA_AYOUB_ING5_ISI.pptx
PRESENTATION_CHAABA_AYOUB_ING5_ISI.pptxMoulayAyoubChaaba
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 

La actualidad más candente (20)

Spring boot
Spring bootSpring boot
Spring boot
 
Unit testing framework
Unit testing frameworkUnit testing framework
Unit testing framework
 
Junit
JunitJunit
Junit
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
 
Google test training
Google test trainingGoogle test training
Google test training
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applications
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
 
PRESENTATION_CHAABA_AYOUB_ING5_ISI.pptx
PRESENTATION_CHAABA_AYOUB_ING5_ISI.pptxPRESENTATION_CHAABA_AYOUB_ING5_ISI.pptx
PRESENTATION_CHAABA_AYOUB_ING5_ISI.pptx
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 

Similar a Mockito

Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaErick M'bwana
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etcYaron Karni
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuPhat VU
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit testEugenio Lentini
 
[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...DevDay.org
 
Summit 16: Stop Writing Legacy Code!
Summit 16: Stop Writing Legacy Code!Summit 16: Stop Writing Legacy Code!
Summit 16: Stop Writing Legacy Code!OPNFV
 
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeMicrosoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeAleksandar Bozinovski
 
Automated testing of ASP .Net Core applications
Automated testing of ASP .Net Core applications Automated testing of ASP .Net Core applications
Automated testing of ASP .Net Core applications nispas
 
Prototype design patterns
Prototype design patternsPrototype design patterns
Prototype design patternsThaichor Seng
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android ApplicationsRody Middelkoop
 
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!Ortus Solutions, Corp
 

Similar a Mockito (20)

Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etc
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
[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...
 
Mock with Mockito
Mock with MockitoMock with Mockito
Mock with Mockito
 
EasyMock for Java
EasyMock for JavaEasyMock for Java
EasyMock for Java
 
Summit 16: Stop Writing Legacy Code!
Summit 16: Stop Writing Legacy Code!Summit 16: Stop Writing Legacy Code!
Summit 16: Stop Writing Legacy Code!
 
Mock your way with Mockito
Mock your way with MockitoMock your way with Mockito
Mock your way with Mockito
 
Easymock
EasymockEasymock
Easymock
 
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeMicrosoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable Code
 
Unit Tests with Microsoft Fakes
Unit Tests with Microsoft FakesUnit Tests with Microsoft Fakes
Unit Tests with Microsoft Fakes
 
Mocking with Mockito
Mocking with MockitoMocking with Mockito
Mocking with Mockito
 
Easy mock
Easy mockEasy mock
Easy mock
 
Automated testing of ASP .Net Core applications
Automated testing of ASP .Net Core applications Automated testing of ASP .Net Core applications
Automated testing of ASP .Net Core applications
 
Ch11lect1 ud
Ch11lect1 udCh11lect1 ud
Ch11lect1 ud
 
Prototype design patterns
Prototype design patternsPrototype design patterns
Prototype design patterns
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android Applications
 
Top Testing Tips
Top Testing TipsTop Testing Tips
Top Testing Tips
 
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!
 

Último

DLL Catch Up Friday March 22.docx CATCH UP FRIDAYS
DLL Catch Up Friday March 22.docx CATCH UP FRIDAYSDLL Catch Up Friday March 22.docx CATCH UP FRIDAYS
DLL Catch Up Friday March 22.docx CATCH UP FRIDAYSTeacherNicaPrintable
 
Material Remains as Source of Ancient Indian History & Culture.ppt
Material Remains as Source of Ancient Indian History & Culture.pptMaterial Remains as Source of Ancient Indian History & Culture.ppt
Material Remains as Source of Ancient Indian History & Culture.pptBanaras Hindu University
 
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdf
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdfPHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdf
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdfSumit Tiwari
 
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdfArti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdfwill854175
 
Alamkara theory by Bhamaha Indian Poetics (1).pptx
Alamkara theory by Bhamaha Indian Poetics (1).pptxAlamkara theory by Bhamaha Indian Poetics (1).pptx
Alamkara theory by Bhamaha Indian Poetics (1).pptxDhatriParmar
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...Nguyen Thanh Tu Collection
 
ICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdfICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdfVanessa Camilleri
 
The First National K12 TUG March 6 2024.pdf
The First National K12 TUG March 6 2024.pdfThe First National K12 TUG March 6 2024.pdf
The First National K12 TUG March 6 2024.pdfdogden2
 
Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptxMetabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptxDr. Santhosh Kumar. N
 
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...gdgsurrey
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...Nguyen Thanh Tu Collection
 
30-de-thi-vao-lop-10-mon-tieng-anh-co-dap-an.doc
30-de-thi-vao-lop-10-mon-tieng-anh-co-dap-an.doc30-de-thi-vao-lop-10-mon-tieng-anh-co-dap-an.doc
30-de-thi-vao-lop-10-mon-tieng-anh-co-dap-an.docdieu18
 
THYROID HORMONE.pptx by Subham Panja,Asst. Professor, Department of B.Sc MLT,...
THYROID HORMONE.pptx by Subham Panja,Asst. Professor, Department of B.Sc MLT,...THYROID HORMONE.pptx by Subham Panja,Asst. Professor, Department of B.Sc MLT,...
THYROID HORMONE.pptx by Subham Panja,Asst. Professor, Department of B.Sc MLT,...Subham Panja
 
Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024bsellato
 
LEAD6001 - Introduction to Advanced Stud
LEAD6001 - Introduction to Advanced StudLEAD6001 - Introduction to Advanced Stud
LEAD6001 - Introduction to Advanced StudDr. Bruce A. Johnson
 
Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchRushdi Shams
 
3.12.24 The Social Construction of Gender.pptx
3.12.24 The Social Construction of Gender.pptx3.12.24 The Social Construction of Gender.pptx
3.12.24 The Social Construction of Gender.pptxmary850239
 
LEAD5623 The Economics of Community Coll
LEAD5623 The Economics of Community CollLEAD5623 The Economics of Community Coll
LEAD5623 The Economics of Community CollDr. Bruce A. Johnson
 
Pharmacology chapter No 7 full notes.pdf
Pharmacology chapter No 7 full notes.pdfPharmacology chapter No 7 full notes.pdf
Pharmacology chapter No 7 full notes.pdfSumit Tiwari
 

Último (20)

DLL Catch Up Friday March 22.docx CATCH UP FRIDAYS
DLL Catch Up Friday March 22.docx CATCH UP FRIDAYSDLL Catch Up Friday March 22.docx CATCH UP FRIDAYS
DLL Catch Up Friday March 22.docx CATCH UP FRIDAYS
 
Material Remains as Source of Ancient Indian History & Culture.ppt
Material Remains as Source of Ancient Indian History & Culture.pptMaterial Remains as Source of Ancient Indian History & Culture.ppt
Material Remains as Source of Ancient Indian History & Culture.ppt
 
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdf
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdfPHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdf
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdf
 
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdfArti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
 
Alamkara theory by Bhamaha Indian Poetics (1).pptx
Alamkara theory by Bhamaha Indian Poetics (1).pptxAlamkara theory by Bhamaha Indian Poetics (1).pptx
Alamkara theory by Bhamaha Indian Poetics (1).pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...
 
ICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdfICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdf
 
The First National K12 TUG March 6 2024.pdf
The First National K12 TUG March 6 2024.pdfThe First National K12 TUG March 6 2024.pdf
The First National K12 TUG March 6 2024.pdf
 
Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptxMetabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
 
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
 
30-de-thi-vao-lop-10-mon-tieng-anh-co-dap-an.doc
30-de-thi-vao-lop-10-mon-tieng-anh-co-dap-an.doc30-de-thi-vao-lop-10-mon-tieng-anh-co-dap-an.doc
30-de-thi-vao-lop-10-mon-tieng-anh-co-dap-an.doc
 
THYROID HORMONE.pptx by Subham Panja,Asst. Professor, Department of B.Sc MLT,...
THYROID HORMONE.pptx by Subham Panja,Asst. Professor, Department of B.Sc MLT,...THYROID HORMONE.pptx by Subham Panja,Asst. Professor, Department of B.Sc MLT,...
THYROID HORMONE.pptx by Subham Panja,Asst. Professor, Department of B.Sc MLT,...
 
Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024
 
Problems on Mean,Mode,Median Standard Deviation
Problems on Mean,Mode,Median Standard DeviationProblems on Mean,Mode,Median Standard Deviation
Problems on Mean,Mode,Median Standard Deviation
 
LEAD6001 - Introduction to Advanced Stud
LEAD6001 - Introduction to Advanced StudLEAD6001 - Introduction to Advanced Stud
LEAD6001 - Introduction to Advanced Stud
 
Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
3.12.24 The Social Construction of Gender.pptx
3.12.24 The Social Construction of Gender.pptx3.12.24 The Social Construction of Gender.pptx
3.12.24 The Social Construction of Gender.pptx
 
LEAD5623 The Economics of Community Coll
LEAD5623 The Economics of Community CollLEAD5623 The Economics of Community Coll
LEAD5623 The Economics of Community Coll
 
Pharmacology chapter No 7 full notes.pdf
Pharmacology chapter No 7 full notes.pdfPharmacology chapter No 7 full notes.pdf
Pharmacology chapter No 7 full notes.pdf
 

Mockito

  • 1. copyright 2008 trainologic LTD Unit testing in a real-life environment using Mockito Stubbing and Mocking
  • 2. copyright 2008 trainologic LTD Stubbing and Mocking • Mediator Objects and Testing • Stubs • Mocking Objects • Mockito • PowerMockito
  • 3. 3 copyright 2008 trainologic LTD • The JUnit testing framework, makes the testing of an independent, non-void method, simple. • But... how do we test an method with no return value? • How do we test a method that interacts with other domain objects? Testing Mediator Objects 3 Mediator Objects & Testing
  • 4. 4 copyright 2008 trainologic LTD Mediator Objects & Testing • Dependencies – In order for a function to run it often requires Files, DB, JNDI or generally – other units • Side effects – As a function runs we are often interested in data written to files, saved to db or generally – passed to other units How can we test a single unit, without being affected by other units – their errors, set-up complexities and performance issues? In Real-Life Units are NOT Totally Self Contained 4
  • 5. 5 copyright 2008 trainologic LTD • Let’s say we want to test a method that gets an Account and a Financial Transaction, checks whether the Transaction is legitimate and only if it is, applies it to the Account. • How would you implement this test? Testing Mediator Objects 5 Mediator Objects & Testing
  • 6. 6 copyright 2008 trainologic LTD • What if the method receives an Account and prints specific parts of it using a Logger? Testing Mediator Objects 6 Mediator Objects & Testing
  • 7. 7 copyright 2008 trainologic LTD • Many business objects that are really important to test are mediators, i.e., they contain methods which interact with other domain objects. • Since in unit testing we want to test a unit without its dependencies, we will have to eliminate them somehow. • We do this by passing dummy objects to “fill in” for the real dependencies. Testing Mediator Objects 7 Mediator Objects & Testing
  • 8. copyright 2008 trainologic LTD Stubbing and Mocking • Mediator Objects and Testing • Stubs • Mocking Objects • Mockito • PowerMockito
  • 9. 9 copyright 2008 trainologic LTD • A Stub is a test-only object which: • Implements the same interface as the production object • Gives the set of services used by the tested unit – often using a naïve, hard-coded implementation • More complex stubs may even contain test-supporting logic, e.g. assert that they are used correctly What is a Stub? 9 Stubs
  • 10. copyright 2008 trainologic LTD • BookInventory is a package for management of … book inventories. • It does inventory management, connection to book catalogs (like library of congress), etc. Exercise 1- BookInventory Stubs
  • 11. 11 copyright 2008 trainologic LTD • Main Classes Exercise 1- BookInventory 11 Stubs 1111 BookInventory Main entry point BookCatalog Get title data Amazon Get title data LibraryOfCongress Get title data ChristianCatalog Get title data BookCopy Main entry point *
  • 12. 12 copyright 2008 trainologic LTD • Write a unit test for BookInventory.registerCopy • A “good” copy can be registered • Several copies can be registered for both same and different titles • A copy can only be registered if the title details are known • Same copy cannot be registered twice • Same copy id for two different titles gives specific error message Exercise 1- BookInventory 12 Stubs
  • 13. 13 copyright 2008 trainologic LTD • Pros: • We can write anything • Cons: • A lot of work • Error prone • Gets complicated as our demands increase: • Was the catalog actually called? • Was it called more than once? • Was the isbn passed correctly to the catalog? • What if two catalogs return different results? Stubbing Pros and Cons 13 Stubs
  • 14. copyright 2008 trainologic LTD Stubbing and Mocking • Mediator Objects and Testing • Stubs • Mocking Objects • Mockito • PowerMockito
  • 15. 15 copyright 2008 trainologic LTD • A Mock object is an object that: • Is created by the mock framework • Implements the same interface as the original dependent object. Otherwise, the compiler won’t let us pass it to the tested object. • Supplies the tested code with everything it expects from the dependent object. • Allows us to check that the tested code is using the dependent object (the Mock) correctly. Mock Objects 15 Mocks
  • 16. 16 copyright 2008 trainologic LTD • To use a Mock object we should: • Create instance of the Mock. • Define bahavior during test. • Invoke the tested code while passing the Mock as parameter. • Verify the Mock has been used correctly. • Let’s see an example... Introduction to Mock Objects 16 Mocks
  • 17. 17 copyright 2008 trainologic LTD 17 Mocks Example: Testing book addition with Mock Title Catalog Construct Set behavior Verify behavior
  • 18. 18 copyright 2008 trainologic LTD • Setup mocks for all dependencies required by our units – Either as part of setUp (@Before) or at the beginning of the test method • Set mocks to correctly handle interaction – Usually at the beginning of the test method • Call business logic under testing • Verify results & behavior (i.e. “side effects”) The Typical Usage Pattern 18 Mocks
  • 19. 19 copyright 2008 trainologic LTD • When we set expectations on unit behavior, we are also setting restrictions on how it is implemented • It is very easy to create tests that are implementation specific, and will break even though the unit is OK • Such tests are called “brittle” and they are nightmare to maintain The danger of testing behavior through mocks 19 Mocks
  • 20. 20 copyright 2008 trainologic LTD • Testing the SQL passed to the DB • Verifying that the unit actually pulls data given by mocks • Testing for a specific order of calls to mocks • Etc. Some Examples of Brittle tests 20 Mocks Over-Specified behavior == Brittle tests
  • 21. copyright 2008 trainologic LTD Stubbing and Mocking • Mediator Objects and Testing • Stubs • Mocking Objects • Mockito • PowerMockito
  • 22. 22 copyright 2008 trainologic LTD Mockito 22 • An open-source project providing an easy way to work with Mock objects. • Can be freely downloaded from Google Code http://code.google.com/p/mockito. • Released under the MIT License. Mockito
  • 23. 23 copyright 2008 trainologic LTD • No need to write Mock objects by hand. • Simple “Set-Run-Verify” work model (as opposed to (expect/record-run-verify) • Refactoring-safe, no need to refactor Mock in case the interface has changed. • Supports return values and Exceptions. • Flexible parameter handling in both set & verify • Single jar, easy setup • Easy to learn Mockito - Benefits 23 Mockito
  • 24. 24 copyright 2008 trainologic LTD • Import Mockito – preferably static: import static org.mockito.Mockito.*; •Option 1: “mock” instead of “new” IAccount mockAct= mock(IAccount.class);// or even… Account mockAct= mock(Account.class); //on a concrete class •Option 2: @mock a member @mock private Account mockAct; (But then you need to use MockitoAnnotations.initMocks or JUnitMockitoRunner) Constructing Mocks 24 Mockito
  • 25. 25 copyright 2008 trainologic LTD • Examples: when(mockedList.get(0)).thenReturn("first"); when(mockedList.get(or(eq(1), eq(2)))). thenReturn(“more"); when(mockedList.get(gt(2)).thenThrow(new RuntimeException()); • Other Matchers: • anyInt, anyString,… • Custom matchers using hamcrest Setting up Mock Behavior 25 Mockito
  • 26. 26 copyright 2008 trainologic LTD • Verify a function was called: verify(mockedList).clear(); •Verify parameters passed: verify(mockedList).add("one"); verify(mockedList).add(anyString()); •Verify number of invocations: verify(mockedList,atLeastOnce()).add("one"); verify(mockedList,times(3)).add(anyString()); similarly: never, atLeast, atMost •CAUTION: This is the heart of brittle testing! Verifying Behavior 26 Mockito
  • 27. 27 copyright 2008 trainologic LTD • Using Mockito: • Rewrite the tests from Exercise 1 • Also test that: • BookInventory.registerCopy: • Works well with several TitleCatalog objects • Stops searching for book details once it has them • Extend BookInventory to support TitleCatalogs of banned books. Test it. (A copy of a banned book cannot be registered) Exercise 2 27 Mockito
  • 28. 28 copyright 2008 trainologic LTD • Spy allows us to wrap a real object, and perform partial mocking • This is dangerous, but may be relevant with some legacy code. Partial Mocking: Spy Objects 28 Mockito
  • 29. 29 copyright 2008 trainologic LTD • Mockito allows you to verify the order in which methods on mocks were called. • This is yet another example of how you can create extremely brittle tests. Use with caution. Verifying call order 29 Mockito
  • 30. 30 copyright 2008 trainologic LTD • You can make sure a mock was not used through verifyZeroInteractions •You can make sure a mock was not used following a specific verify through verifyNoMoreInteractions •Both are often over specification… Expecting Nothing is Expecting Something 30 Mockito
  • 31. copyright 2008 trainologic LTD Stubbing and Mocking • Mediator Objects and Testing • Stubs • Mocking Objects • Mockito • PowerMockito
  • 32. copyright 2009 Trainologic LTD • PowerMock is a framework that extends Mockito/EasyMock. For Mockito it is called PowerMockito. • PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, and more. • This is a mixed blessing: • We can avoid re-factoring legacy code in order to test it • We are less inclined to fix bad design PowerMock EasyMock
  • 33. copyright 2009 Trainologic LTD • Lets see an example: • We have the following class: PowerMockito Example EasyMock
  • 34. copyright 2009 Trainologic LTD PowerMock Example EasyMock
  • 35. 35 copyright 2008 trainologic LTD EasyMock • In order to unit-test isolated units we must use Mock objects. • Writing Mocks by hand is cumbersome. • Mockito is a simple and easy to use library that helps us create Mock implementations on the fly. How do you know if a person really does unit testing? Ask him what mocking framework he is using! Summary 35